I had the following method for some currency conversion, the program should calculate 50000.0/8185= 6.10873549175321 and round it as 6.11, however i am getting 6.10 no matter what BigDecimal rounding method used.
So after doing some searching on this site, the issues surrounding money are starting to come back… Slowly, really slowly…
Money is strange, since dollars are really just a classification of a grouped amount much like hundreds, thousands, and millions are in normal counting. The deal is that money is really cents. Not dollars. A dollar really = 100 cents. When dealing with a 50 cents you don’t have 1/2 a dollar you have 50 cents. When you have a dollar, you really have 100 cents. When you have a million dollars you really have 100,000,000 (one hundred million) cents.
So the ‘proper’ way to store money is to store how many cents of something you have. Then using formatting you determine how many dollars you have with x amount of cents. So consider storing your money as an Integer instead of a Double or Float which allows rounding. Have you have seen 1/2 a cent? Neither have I because a cent is always a whole number. Storing money as an Integer will also help with calculating exchanges between currencies.
There was a great article or thread on this site talking about money, but I can’t find it. I did find the following threads though:
The first thread has a link to a in-depth discussion about rounding, which might be beneficial to you.
BUT, if you have to store money as a decimal (despite the warning above), I’ll take a look at what is happening in your code a little later tonight or this weekend sometime.
I would have to look closer to see what is going wrong, but if you’re using the above code in a production system you might consider using a money type instead of an integer for keeping track of money.
There are some great articles on this site that explain the use of money types vs integers or even big decimal.
I will have to take time later though to see what the issue is with the rounding.