Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

tostring

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

like image 242
Makach Avatar asked Aug 23 '09 18:08

Makach


People also ask

What is E in double value?

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.


2 Answers

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

string formatted = String.Format("{0:F20}", value);
like image 111
i_am_jorf Avatar answered Oct 09 '22 12:10

i_am_jorf


How about

Convert.ToDecimal(doubleValue).ToString()
like image 21
Zoman Avatar answered Oct 09 '22 11:10

Zoman