How to convert double to string without permission up to 10 (E-05)

How to convert double to string without permission up to 10 (E-05)

double value = 0.000099999999833333343; string text = value.ToString(); Console.WriteLine(text); // 9,99999998333333E-05 

I want the text of the line to be 0.000099999999833333343 (or almost that, I'm not doing rocket :)

I tried the following options

 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 a tostring N20 or F20 format seems the closest to what I want, but in the end I have a lot of trailing zeros, is there a tricky way to avoid this? I would like as close as possible to the double representation of 0.000099999999833333343

+10
tostring c #
Aug 23 '09 at 18:21
source share
4 answers

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

 string formatted = String.Format("{0:F20}", value); 
+11
Aug 23 '09 at 18:25
source share

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

+5
Aug 23 '09 at 18:27
source share

What about

 Convert.ToDecimal(doubleValue).ToString() 
+5
Aug 6 '14 at 18:14
source share

Use string.Format with the appropriate format specifier.

There are many examples in this blog post: http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx

+3
Aug 23 '09 at 18:25
source share



All Articles