开发者

C# fails to parse NaN as a double

开发者 https://www.devze.com 2023-01-05 06:22 出处:网络
On a Windows PC in Japan, this line of C# throws a format exception: double d = double.Parse(\"NaN\");

On a Windows PC in Japan, this line of C# throws a format exception:

double d = double.Parse("NaN");

This line executes fine开发者_开发技巧 on my PC in the U.S.

Don't know where to begin troubleshooting this one. Any thoughts?

Thanks in advance, Jim


I see what the problem is. Try using the invariant format provider.

double d = double.Parse("NaN", CultureInfo.InvariantCulture);


First, you should determine the double value for "NaN". Anyway, parsing non numerical format string will cause System.FormatException, you should catch it and set double value manually.

double x;
string foo = "NaN";
try 
{
    x = double.Parse(foo);
}
catch
{
    x = 0.0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消