Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
The question might sound a bit dumb, but I have been caught in a calculation scenario here.

There is a no. I divide it by a no. then again multiply it by same no. but I do not get exactly the same no. Decimal values are different. Can anyone please tell me a workaround of the problem.
Here is the code
Round1 and Round2 are having .00001 difference.


static void Main(string[] args)
{
decimal ab = new decimal();
ab = (decimal)4.444445;
decimal Round1 = Math.Round(ab,5);
decimal c = ab / 3;
decimal d = c * 3;
decimal Round2=Math.Round(d,5);
}
Posted
Comments
Sergey Alexandrovich Kryukov 31-May-15 13:54pm    
If course they are different. Rounding introduced rounding error. :-)
Any concerns about it?
—SA
Anant Beriwal 31-May-15 23:57pm    
so any solution?? I need to round up. Can I provide some algo or hypothesis to it.
gggustafson 31-May-15 14:00pm    
Math.Round is an abomination! It defers to "oddness" or "evenness" of a result. For that reason I wrote my own. See Known Color Palette Tool (http://www.codeproject.com/Articles/243610/The-Known-Colors-Palette-Tool-Revised-Again) in the section entitled (poorly) Extended Color class. The round function is in that section.
Sascha Lefèvre 31-May-15 14:16pm    
Unrelated suggestion: You don't need to use "new" for primitive types. And you can tell the compiler a floating-point literal should be a decimal by appending an "m". The first two lines are therefore more elegantly written like so (single line):
decimal ab = 4.444445m;
Sergey Alexandrovich Kryukov 31-May-15 14:27pm    
Sure.
—SA

1 solution

Please see my comment to the question.

I would advise the following: never ever round anything up. It just makes no sense, but can compromise the accuracy of calculations. There are cases when rounding error accumulate during calculation steps resulting in the error much greater than your required accuracy. Simply never round any calculations at all.

Instead, round up data only when you represent the final data in the string form, for example, when you show it on screen or store in a text file. At this step, you still don't need rounding itself, because it is already implemented in string formatting. In other words, reduce rounding problem to the formatting problem. Please see:
https://msdn.microsoft.com/en-us/library/system.double.tostring%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/kfsatb94%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx[^].

—SA
 
Share this answer
 
Comments
CHill60 31-May-15 14:30pm    
All QA posts seem to be going through moderation queue. I wasn't quick enough to ok your solution and it's appeared twice - you might want to delete one copy
Sergey Alexandrovich Kryukov 31-May-15 14:41pm    
Thank you very much. I will.
I don't think there is actual moderation. It looks like am old technical glitch, in a new form. When I refresh the page, it looks like non-posted text in the input box. Posting it results in the same situation repeated, or correct post, or double post. I failed to spot exact scenario, but it happened many times before, only without the word "pending".
—SA
CPallini 31-May-15 16:36pm    
5.
Sergey Alexandrovich Kryukov 31-May-15 19:21pm    
Thank you, Carlo.
—SA
Anant Beriwal 31-May-15 23:56pm    
Sir,
The problem is, I have to round of. This is a real time scenario, the database will not hold anything beyond 5 decimal points. truncating the decimal set will cause even more difference with actual answer. I want to know, Can we do it someway correct.

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