Decimal rounding strategies in enterprise applications
Well, I am wondering about a thing with rounding decimals, and storing them in DB.
Problem is like this:
Let's say we have a customer and a invoice.
The invoice has total price of $100.495 (due to some discount percenta开发者_StackOverflow中文版ge which is not integer number), but it is shown as $100.50 (when rounded, just for print on invoice). It is stored in the DB with the price of $100.495, which means that when customer makes a deposit of $100.50 it will have $0.005 extra on the account. If this is rounded, it will appear as $0, but after couple of invoices it would keep accumulating, which would appear wrong (although it actually is not).
What is best to do in this case. Store the value of $100.50, or leave everything as-is?
You should store the number exactly as you want it to be reflected on their balance.
Are you actually charging the customer the half-cent? If you are, it should be reflected in the DB entry. If you're not, then it shouldn't.
But unless I were posting accrued interest daily (like Prosper.com for example) I would stick with units of currency that people can pay (or withdraw) exactly.
It depends what is more important. But ususally if you doing a accounting or invoicing system, conciliation of balances is an important goal. So for this reason I would store the rounded value.
I'd save both (seriously).
Store both. Your customers will not be able to pay the non-rounded amount by credit card anyway, but your books need to be correct.
精彩评论