Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equality with Double.NaN

Tags:

c#

equality

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.

like image 416
Chris Cudmore Avatar asked Feb 17 '09 19:02

Chris Cudmore


People also ask

What does Double NaN mean?

NaN and Double. NaN: “A constant holding a Not-a-Number (NaN) value of type double. It is equivalent to the value returned by Double.

What does Double NaN return?

All numeric operations with NaN as an operand produce NaN as a result. Reason behind this is that NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false.

Can you compare NaN?

Unlike all other possible values in JavaScript, it is not possible to use the equality operators (== and ===) to compare a value against NaN to determine whether the value is NaN or not, because both NaN == NaN and NaN === NaN evaluate to false . The isNaN() function provides a convenient equality check against NaN .


1 Answers

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)); } 
like image 53
Andrew Hare Avatar answered Nov 10 '22 00:11

Andrew Hare