Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What should be the best practice for rounding currency values in order to avoid the difference. e.g, divide 32 by 3 gives 10.6666666667 and if i round this value to 2 decimal places it would give 10.67 now if i multiply this value to 3 it will result in 32.01 this means a difference of 0.01 will appear. How to avoid this difference, plz help
Posted
Updated 11-May-11 23:18pm
v3

Never round any numeric values, to avoid rounding errors, which can event accumulate, depending on calculations.

What you really need is not rounding but just the rounded visual (human-readable) presentation of results in the form of strings, only when you present them on screen. This is a matter of string formatting. All string manipulation libraries have comprehensive ways of setup different kinds of string formatting to desired accuracy.

It it comes to paying some money in cash, where the rounding to the smallest monetary unit is required (such as cents), the rounding should be done only once before the transaction.

—SA
 
Share this answer
 
Comments
ZeeroC00l 12-May-11 3:05am    
My +5 :)
Sergey Alexandrovich Kryukov 12-May-11 3:40am    
Thank you.
--SA
fawadparacha 12-May-11 3:15am    
the solution is quite helpful but the problem is the datatype for storing currency values is setup to numeric(13, 2) (sql server) now if i change it to float this would be a big architecture change which would not be feasible for me at all
Sergey Alexandrovich Kryukov 12-May-11 3:46am    
Yes, this is another, related problem. You need to find the optimal solution on what type to use and what part of system to change. In principle, there is nothing wrong in change of type when moving data from database and processing code, but very unpleasant; the types better match.
You need to make the decision, and this is beyond this question. As you asked about rounding, I think it was float/double type.
--SA
fawadparacha 12-May-11 3:56am    
thank you, i really appreciate your replies, but float/double datatypes are large when it comes to think about memory storage per datatable row, because i have millions of transactional rows therefore i intentionally setup numeric(13, 2) datatype to limit memory storage. i am still looking for any solution
I would do it in a more simpler way..
32 * 100 = 3200

3200/3   = 1066.666666666..7

Round off to 3 Decimal Place
1066.667

1066.667 * 3   = 3200.001

3200.001 / 100 = 32.00001 


This difference of 0.00001 is really small compared to that of 0.01.

This Method might seem a little tedious, but when it comes to currency management this is the best way that I can think of.


BR//
Harsha
 
Share this answer
 
Comments
fawadparacha 12-May-11 2:28am    
thanks harsha

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