[ACCEPTED]-Equality with Double.NaN-nan
Perhaps you are looking for the IsNaN
static 1 function?
Try something like this:
if (!Double.IsNaN(Price_Foreign))
{
output.Append(spacer);
output.Append(String.Format("{0,-10:C} USD",Price_Foreign));
}
The IEEE 754 floating point standard states that comparing NaN with NaN 11 will always return false. If you must do this, use 10 Double.IsNaN()
.
But, this isn't the best way to do what 9 you're trying to do. Doubles are NOT precise, and 8 you're using them to represent prices here. I'm 7 betting that at some point, you're going 6 to want to compare prices for equality, too. That's 5 not going to work, because you can't rely on floating point equality.
You should really 4 look into using some integer type for these 3 values (that supports equality comparison) rather 2 than trying to use doubles. Doubles are 1 for scientific problems; not for finance.
Double.NaN
is not equal to anything, not even itself.
See 6 the Double.NaN Field in the .NET Framework Class Library 5 documentation:
Use IsNaN to determine whether 4 a value is not a number. It is not possible 3 to determine whether a value is not a number 2 by comparing it to another value equal 1 to NaN.
As background information: what the IsNaN()
method 1 does is return v != v;
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.