如何将字符串解析为 Double
这是我的字符串,
20.0e-6
我正在解析它,
String Ans=Double.Parse("20.0e-6")
现在我得到像 2E-05
这样的结果 但所需的输出应该是这样的 0.00002
如何得到这个?
Here is my string
20.0e-6
I'm parsing it like
String Ans=Double.Parse("20.0e-6")
Now i'm getting the result like 2E-05
But the required output should be like0.00002
How to get this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
获得所需结果的一种方法是使用
String.Format
,如下所示:根据您的示例,这将输出值
0.00002
EDIT
I'我刚刚意识到这实际上与您的问题相反,因此为了保持答案有用,我将添加以下内容:
给定一个字符串,您可以解析为 double,然后应用与上面相同的逻辑。可能不是最优雅的解决方案,但它提供了另一种方式来获得您想要的结果。
One way to get the result you want is to use
String.Format
as follow:Given your example, this outputs the value
0.00002
EDIT
I've just realised that this is actually the opposite of your question so in the aim of keeping the answer useful i'll add the following:
Given a string, you can parse as double and then apply the same logic as above. Probably not the most elegant solution however it offers another way to get the result you want.
Double.Parse
的结果是一个Double
,而不是字符串。您需要使用ToString
从双精度数中输出一个字符串。您还应该使用
Double.Parse
具有NumberStyles
参数。使用Float
值允许指数表示法:如果您不想冒异常风险(例如
InvlidCastException
),则可以使用TryParse
:The result of
Double.Parse
is aDouble
, not a string. You need to output a string from the double, usingToString
.You should also use an overload of
Double.Parse
that has aNumberStyles
parameter. Using theFloat
value allows exponent notation:If you don't want to risk exceptions (
InvlidCastException
for example), you can useTryParse
:数字相同,但如果您想修改字符串的输出,请在
ToString()
上使用格式化程序http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
所以
It's the same number, but if you want to modify the output of the string, use a formatter on your
ToString()
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
So