How to convert double to string without the power to 10 representation (E-05)

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


Solution 1:

Use String.Format() with the format specifier. I think you want {0:F20} or so.

string formatted = String.Format("{0:F20}", value);

Solution 2:

How about

Convert.ToDecimal(doubleValue).ToString()

Solution 3:

You don't need string.Format(). Just put the right format string in the existing .ToString() method. Something like "N" should do.

Solution 4:

Use string.Format with an appropriate format specifier.

This blog post has a lot of examples: http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx