简体   繁体   English

Double.Parse(“NaN”)是否正确解析?

[英]Does Double.Parse(“NaN”) parse correctly?

Does the C# Double parse "NaN" correctly, ie. C# Double是否正确解析“NaN”,即。 does Double.Parse("NaN").ToString() == "NaN" ? Double.Parse("NaN").ToString() == "NaN"

Try it: 试试吧:

PS> $ic = [Globalization.CultureInfo]::InvariantCulture
PS> [double]::parse('NaN', $ic).ToString($ic)
NaN

However, parsing and output of this string is locale-dependent, so either make sure you're always passing a culture or don't make too many assumptions about the format. 但是,此字符串的解析和输出依赖于语言环境,因此要么确保始终传递文化,要么不要对格式做太多假设。

var d = Double.Parse("NaN");
Console.WriteLine(d); // prints "NaN"

I'm running under the en-US locale. 我在en-US区域运行。 As Joey notes, be careful around this. 正如乔伊所说,要小心这一点。 I got the same results when I specified the invariant culture (as one often should in these circumstances): 当我指定不变文化时(在这种情况下经常应该这样),我得到了相同的结果:

var d = Double.Parse("NaN", CultureInfo.InvariantCulture);
Console.WriteLine(d.ToString(CultureInfo.InvariantCulture)); // prints "NaN"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM