为什么double.TryParse(“0.0000”,out doubleValue)返回false? [英] Why double.TryParse("0.0000", out doubleValue) returns false ?

查看:200
本文介绍了为什么double.TryParse(“0.0000”,out doubleValue)返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 double.TryParse()解析字符串0.0000,但是我不知道为什么在这个特定的例子中它会返回false。当我传递类似整数的字符串,例如5它正确解析为5的价值。



任何想法为什么发生?

解决方案

它将运行时的本地化设置考虑在内...也许您正在系统上运行此功能,其中不是小数点,而是而是...



你的具体情况我假定你想要一个固定的文化,不管你运行的系统与作为小数点:

  double.TryParse(0.0000,NumberStyles.Number,CultureInfo.CreateSpecificCulture(en-US),out temp)
/ pre>

  double.TryParse(0.0000 NumberStyles.Number,CultureInfo.InvariantCulture,out temp)

一些MSDN参考链接:




I am trying to parse string "0.0000" with double.TryParse() but I have no idea why would it return false in this particular example. When I pass integer-like strings e.g. "5" it parses correctly to value of 5 .

Any ideas why it is happening ?

解决方案

it takes the localization settings at runtime into account... perhaps you are running this on a system where . is not the decimal point but , instead...

In your specific case I assume you want a fixed culture regardless of the system you are running on with . as the decimal point:

double.TryParse("0.0000", NumberStyles.Number, CultureInfo.CreateSpecificCulture ("en-US"), out temp)

OR

double.TryParse("0.0000", NumberStyles.Number,CultureInfo.InvariantCulture, out temp)

Some MSDN reference links:

这篇关于为什么double.TryParse(“0.0000”,out doubleValue)返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆