Denis Biondic
Denis Biondic

Reputation: 8201

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

Upvotes: 3

Views: 798

Answers (4)

John
John

Reputation: 16007

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.

Upvotes: 5

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120937

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.

Upvotes: 1

ChristopheD
ChristopheD

Reputation: 116157

I'd save both (seriously).

Upvotes: 1

AxelEckenberger
AxelEckenberger

Reputation: 16926

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.

Upvotes: 1

Related Questions