简体   繁体   中英

double.ToString() return wrong value in C#

I have double variable and its value is :

double d = 0.000000000000056843418860808015;

when i print this variable its print wrong.

d.ToString();

Output : "5.6843418860808E-14"

How to resolve this?

Well if you want to have an output without the exponential notation you need to format your string:

d.toString("F25");

This will give you the "correct" number with up to 25 fractional digits.

0,0000000000000568434188608

Edit : Complete list of formats and conversions are available here and snapshot for you is below.

Original value: 1054.32179

F:                     1054.32 
F0:                    1054 
F1:                    1054.3 
F2:                    1054.32 
F3:                    1054.322
        double d = 0.000000000000056843418860808015;
        var str = d.ToString("G17");
        var value = str.Split('E')[0];
        var zeros = Int32.Parse(str.Split('E')[1]);
        var outputString = "";
        zeros = Math.Abs(zeros);
        var addstring = "0.";
        for (var i = 0; i < zeros - 1; i++)
        {
            addstring += '0';
        }
        value = value.Remove(1, 1);
        value = addstring + value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM