Storing Money Values: What Actually Works
How to represent prices and amounts in code and databases without rounding errors.
On this page
Why does it matter?
Floating point arithmetic loses cents, and rounding bugs in money are noticed immediately by customers and accountants.
What are the numbers?
- Floating point binary floats cannot represent many decimal values exactly
- Integer cents storing minor units avoids fractions
- Decimal types databases offer fixed-precision decimal columns
- Currencies not all currencies have two decimal places
What should I do?
- Store amounts as integers in minor units or decimals
- Keep currency alongside every amount
- Round only at defined points
- Use well-tested money libraries
- Test totals, taxes and discounts
What should I avoid?
Avoid:
- Floats for money
- Currency assumed from context
- Rounding at every calculation step
- Custom rounding logic without tests
When should I get help?
Short answer Bring in help when building billing or accounting features.
Where this comes from
- PHP Manual — Floating point numbers
- Microsoft Learn — Decimal data types
The figures and practices above come from the sources listed.
Working on something like this?
We take on Web Design & Development work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.
Where to go next
Spotted something wrong? Report an error on this page. We correct on the page and say what changed.