How to convert double to string without the power to 10 representation (E-05)
double value = 0.000099999999833333343;
string text = value.ToString();
Console.WriteLine(text); // 9,99999998333333E-05
I'd like the string text to be 0.000099999999833333343 (or nearly that, I'm not doing rocket science:)
I've tried the following variants
Console.WriteLine(value.ToString()); // 9,99999998333333E-05
Console.WriteLine(value.ToString("R20")); // 9,9999999833333343E-05
Console.WriteLine(value.ToString("N20")); // 0,00009999999983333330
Console.WriteLine(String.Format("{0:F20}", value)); // 0,00009999999983333330
Doing tostring N20 or format F20 seems closest to what I want, but I do end up with a lot of trailing zeros, is there a clever way to avoid this? I'd like to get as close to the double representation as possible 0.000099999999833333343
In scientific notation, the letter E is used to mean "10 to the power of." For example, 1.314E+1 means 1.314 * 101 which is 13.14 . Scientific notation is merely a format used for input and output. The 64-bit pattern used for a double inside the computer are the same, no matter what character format was used for input.
Use String.Format() with the format specifier. I think you want {0:F20} or so.
string formatted = String.Format("{0:F20}", value);
How about
Convert.ToDecimal(doubleValue).ToString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With