views:

160

answers:

4

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 percentage 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?

+1  A: 

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.

Obalix
+1  A: 

I'd save both (seriously).

ChristopheD
+1  A: 

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.

klausbyskov
+5  A: 

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.

John at CashCommons
No, we are not charging for that half-cent, because invoice says $100.50, and that is the amount that customer deposits. The system is used almost exclusively just for customer balances, there are no balance sheets, cash flow statements, or anything other heavily accounting-wise.What do you suggest then, to round it, and store it like that?
Sapphire
In that case, definitely store the rounded amount. If the customer buys one, charge (and store) $100.50 (or $100.49, however you want to round the whole amount). If they buy two, charge and store $200.99. In your case the half-cent doesn't matter after the order is done, so forget about it.
John at CashCommons
Whatever your business rules are, that's what you should store. If you charge $100.50 and the customer paid $100.50, storing $100.495 is worthless to everyone involved. Just like if it came to 100.3333333333 you wouldn't store all of the threes, your business rules would tell you how and when to cut it off.
Chris Haas
Thank you very much, that cleared all my doubts.
Sapphire