5

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

Jim C
  • 4,517
  • 7
  • 29
  • 33

2 Answers2

7

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

double d = double.Parse("NaN", CultureInfo.InvariantCulture);
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
-3

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;
}
fish potato
  • 5,319
  • 6
  • 27
  • 32
  • 1
    Actually `TryParse` is the preferred method as it does not rely on an exception for control flow. – ChaosPandion Jul 02 '10 at 02:21
  • 1
    But the variable needs to be initialized to Double.NaN. This value exists in a file that is initializing the double. – Jim C Jul 02 '10 at 02:21