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

不羁岁月 提交于 2019-11-26 16:46:12

问题


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


回答1:


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

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



回答2:


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




回答3:


How about

Convert.ToDecimal(doubleValue).ToString()



回答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



来源:https://stackoverflow.com/questions/1319191/how-to-convert-double-to-string-without-the-power-to-10-representation-e-05

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!