Skip to content
Web Design & Development

Storing Money Values: What Actually Works

How to represent prices and amounts in code and databases without rounding errors.

MediaScaleUp Editorial Team 2 min read 17 views
Storing Money Values: What Actually Works

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?

  1. Store amounts as integers in minor units or decimals
  2. Keep currency alongside every amount
  3. Round only at defined points
  4. Use well-tested money libraries
  5. Test totals, taxes and discounts
What to do and what to avoid with storing money values, side by side
Good practice against the usual mistakes, from the sources listed below.

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

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.

All services

The work behind this article, and what it costs.

MediaScaleUp Editorial Team

Articles here are researched, written and checked by the MediaScaleUp studio team before they are published, and dated when they are reviewed.

Keep reading

More in Web Design & Development