Equality with Double.NaN

Chris Cudmore picture Chris Cudmore · Feb 17, 2009 · Viewed 17.8k times · Source

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.

Answer

Andrew Hare picture Andrew Hare · Feb 17, 2009

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));
}