简体   繁体   中英

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

Does the C# Double parse "NaN" correctly, ie. does 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. 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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