static void Main(string[] args)
{
double y = 23.12F;
Console.Wri开发者_如何学PythonteLine(y);
Console.Read();
}
why value of y shows 23.1200008392334 instead of 23.12
static void Main(string[] args)
{
int i = -9; uint j = (uint)i;
Console.WriteLine(j);
Console.Read();
}
How j shows value 4294967287
Floating points are only guaranteed to be exact for integer values; beyond that, they cannot represent all numbers; 23.1200008392334 is the closest it can get to 23.12, due to how IEEE754 works.
If you want "typical" handing of decimal values... use decimal
instead:
decimal y = 23.12M;
With the uint
- because uint
doesn't have negative values. Instead, you are tracking backwards from the maximum value of uint
. If you don't want this, use checked
:
uint j = checked((uint) i);
or
checked
{
uint j = (uint)i;
}
then it will explode in a shower of sparks instead. Sometimes a good thing; sometimes bad. unchecked
is the default.
According to MSDN 'that's just how double works' http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/1fcf6486-807d-4dce-8aef-7fe5268b568d/
I know that this is Java, but very similar to C# : Convert float to double without losing precision
精彩评论