Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am using math.round function.. in which i provide 1493501.1*5/100*100/100 this for math.round function... this function should return this values 74675.06 instead of 74675.05....???
Posted
Updated 28-Jul-14 22:29pm
v2
Comments
_Amy 29-Jul-14 4:18am    
What is your actual value?
OriginalGriff 29-Jul-14 4:20am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So we have no idea what datatypes you are using, what values they contain, how you know what values they contain, and how you are rounding it.
Without that, we would have to guess!
Show us the code fragment and the inputs it takes, the outputs it gives.
Use the "Improve question" widget to edit your question and provide better information.
rhl4569 29-Jul-14 4:26am    
sorry sir

I would recommend you to read this excellent thread:
Why does Math.Round(2.5) return 2 instead of 3 in C#?[^]


You should be aware of:
Math.Ceiling()[^]
Math.Floor()[^]
Math.Round[^]

--Amy
 
Share this answer
 
This is actually very interesting, since normally, the round to nearest will round 74675.055 to 74675.06, but in this case it goes the other way...
I broke the calculation into steps and printed the result at each step:
C#
double foo = 1493501.1;
Console.WriteLine(foo.ToString("R"));
foo *= 5;
Console.WriteLine(foo.ToString("R"));
foo /= 100;
Console.WriteLine(foo.ToString("R"));
foo *= 100;
Console.WriteLine(foo.ToString("R"));
foo /= 100;
Console.WriteLine(foo.ToString("R"));
foo = Math.Round(foo, 2);
Console.WriteLine(foo.ToString("R"));

I used the "R" round-trip format to get the most information from the output.
The result was:
1493501.1
7467505.5
74675.055
7467505.4999999991
74675.055
74675.05

After the foo *= 100; step you can see that the value is slightly less than 7467505.5, so I believe that it remains ever so slightly below the value displayed (not enough to change what is displayed, but enough to change what is calculated by Math.Round).
Converting from double to decimal gives exactly the result you expect.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Jul-14 0:39am    
Voted 4. Those are interesting observations, but there are two more important aspects of that: 1) there are few alternative rounding methods, without clear "absolute" preference of just one of them; 2) the approach of avoiding any rounding in the application program at all.
Please see my answer.
—SA
First of all, there are different rounding rules related to so called tie-breaking. Here is the overview and some considerations one need to put in evaluation of them:
http://en.wikipedia.org/wiki/Rounding#Tie-breaking[^].

So, I am not sure that your preference you present to us as something granted actually makes sense.

Another problem with your approach is this: the whole idea of explicitly rounding any calculation might be wrong. It's very likely that in fact you don't need any rounding, but you need to present some numbers in a rounded way on screen or some string. Surprisingly, such presentation, being based on rounding, is isolated from the rounding and is reduced to number formatting:
http://msdn.microsoft.com/en-us/library/system.double.tostring%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/kfsatb94%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx[^].

Real explicit rounding should be used rarely, in some very special cases, like custom random number generators. It's actually a good idea to refrain from rounding and opt for formatting only. This way, you avoid the risk of rounding off some intermediate results of calculations, which can lead to unacceptable precision loss in final results, or, in some case, even in accumulation of errors. The Wikipedia article I referenced first can give you some idea on why it may happen.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900