Saar Carmi

A .NET Blog

<August 2008>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456


Navigation

Subscriptions

Post Categories



Decimal Percision - short code, strange problem

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/867f655285c0e04d/8a59fb7d2904c775?tvc=1&q=saar+decimal#8a59fb7d2904c775Could anyone help me to understand why in the following code sum1 is different from sum2?
Basically it's the same calculation done. Keep in mind the x is 28 digits so it's within the range of decimal.


decimal  x = (-0.0084682975822291150192357277M);
decimal sum1 , sum2;

sum2 = (3500.91M * x) + (x * 3500M + 822m);
sum1 = (3500.91M * x) + (x * 3500M) + 822m;

Console.WriteLine (sum1);
Console.WriteLine (sum2);

------------

Well, the reason is that the decimal type precision s not 28 right to the decimal digit, but 28 digits overall. Thanks for those who answered.

posted on Monday, February 06, 2006 12:37 PM by saarc





Powered by Dot Net Junkies, by Telligent Systems