如何双转换成字符串没有权力再10 presentation(E-05) [英] How to convert double to string without the power to 10 representation (E-05)
问题描述
如何双转换成字符串没有权力再10 presentation(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
我想字符串的文本的是0.000099999999833333343(或接近的是,我没有做火箭科学:)
I'd like the string text to be 0.000099999999833333343 (or nearly that, I'm not doing rocket science:)
我试过以下的变种
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
做的toString N20或格式F20似乎最接近我想要什么,但我做了很多尾随零的结束,有一个聪明的方法来避免这种情况?我想获得尽可能接近双重新presentation尽可能0.000099999999833333343
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
推荐答案
使用的String.Format()与格式说明。我想你想要的。{0:F20}左右。
Use String.Format() with the format specifier. I think you want {0:F20} or so.
string formatted = String.Format("{0:F20}", value);
这篇关于如何双转换成字符串没有权力再10 presentation(E-05)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!