Search code examples
c#equalitynan

Equality with Double.NaN


I have the following code...

if (Price_Foreign != Double.NaN)
{
   output.Append(spacer);
   output.Append(String.Format("{0,-10:C} USD",Price_Foreign));
}

Which outputs:

NaN USD

What gives?

I'm using Double.NaN to indicate that the value doesn't exist, and shouldn't be output.


Solution

  • Perhaps you are looking for the IsNaN static function?

    Try something like this:

    if (!Double.IsNaN(Price_Foreign))
    {
       output.Append(spacer);
       output.Append(String.Format("{0,-10:C} USD",Price_Foreign));
    }