将双精度数转换为字符串给出意外的输出
我有,
double d = 0.005;
d = d/100;
string str = Convert.ToString(d);
output of str = 5E-05
但我需要输出为 0.00005 而转换为字符串 0.00005 则变为 5E-05。
我该如何解决?
I have,
double d = 0.005;
d = d/100;
string str = Convert.ToString(d);
output of str = 5E-05
But I need output as 0.00005
While converting to string 0.00005 become 5E-05.
How can I resolve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要一个 IFormatProvider:
http://www.csharp-examples.net/iformatprovider-numbers/< /a>
http://msdn.microsoft.com/en-us/library/7tdhaxxa.aspx
编辑:上面的海报为您提供了更多详细信息。
这应该编译并显示您想要的内容。
没有比这个
编辑更清楚的了:
更具体的例子请看这里:`
`
You need a IFormatProvider:
http://www.csharp-examples.net/iformatprovider-numbers/
http://msdn.microsoft.com/en-us/library/7tdhaxxa.aspx
EDIT: The above poster give you more details.
This should compile and show what you want.
It can't get any more clear than this
Edit:
For more concrete examples look here: `
`
您想要指定将双精度数转换为字符串的格式。
Double.ToString
不允许您这样做(它使用科学计数法),因此您应该使用String.Format
。这是您的代码,已更新:
string str = String.Format("{0:0.#####}", 0.00005);
事实上,Double.ToString 实际上在兜帽。有关更多信息,请参阅此链接:MSDN有关
Double.ToString
的文档请参阅以下链接以获取
String.Format
的更多示例:使用
String.Format
的示例You want to specify a format with which to convert the double to a string.
Double.ToString
does not let you do that (it uses scientific notation), so you should useString.Format
, instead.Here's your code, updated:
string str = String.Format("{0:0.#####}", 0.00005);
In fact, Double.ToString actually uses String.Format under the hood. See this link for more: MSDN docs about
Double.ToString
Please see the following link for more examples of
String.Format
:Examples of using
String.Format
我找到了解决方案:
它工作正常。但我不知道这到底是做什么的。这适用于动态双值。
谢谢两位的回复。
I found the solution :
It is working fine. But what exactly this is doing that I don't know. This working for dynamic double value.
Thnks for both of you, who replied.