Skip to main content

Command Palette

Search for a command to run...

How to Represent Money in Software

Updated
4 min read
How to Represent Money in Software

Quick Answer: Never use floating-point numbers to represent money in software. Because floating-point math cannot perfectly represent base-10 decimals, it introduces microscopic inaccuracies that compound over time. I recommend using either a dedicated arbitrary-precision decimal library or storing currency as integers representing micro-units to guarantee exact calculations.

If I ask you how to represent £1.29 in code, your naive first instinct might be to just drop it into a floating-point variable. I see this often, but if you start representing money as floating points, you are going to introduce systemic inaccuracies into your application. If there is one thing I know for sure, it's that people really don't like having inaccuracies anywhere close to their money. Let's look at why this happens and what you should do instead.

Why do floating-point numbers fail for currency calculations?

Floating-point numbers fail because they use binary fractions to approximate base-10 decimals, meaning simple arithmetic often yields imprecise results. When you execute a long sequence of these operations, those tiny inaccuracies compound into real financial errors.

I have a whole other video on why 0.1 + 0.2 doesn't equal 0.3 in most programming languages, but it's the classic example of this problem. If you run that calculation using standard floating-point operations, you'll get something like 0.30000000000000004.

// This is why floats and money don't mix
const itemOne = 0.1;
const itemTwo = 0.2;
console.log(itemOne + itemTwo === 0.3); // Evaluates to false

If your business logic relies on checking if those two items total exactly 0.3, the code evaluates to false. Do this thousands of times across a financial system, and those compounding rounding errors eventually change the amount of money a user actually has.

What are the best methods to handle money in programming?

The two reliable methods for handling money are using an arbitrary-precision decimal library or representing the currency as an integer. Decimal libraries give you exact math natively, while the integer method relies on fast, reliable whole-number operations.

Most modern languages include built-in libraries capable of doing accurate decimal operations. These are great because they solve the problem entirely at the language level. However, they can be somewhat heavyweight and a bit slower to execute. If you want an alternative that keeps things lightweight and fast, you can use integers instead.

Here is how the two viable strategies compare:

  • Built-in Decimal Libraries: Provide perfect mathematical accuracy and high readability, but are heavier in memory and slower for the CPU to compute.
  • Integer Representation: Extremely fast for the CPU to process and highly accurate, but requires manual mathematical scaling logic before rendering values to the UI.

How do you implement the integer micro-unit pattern?

To use the integer micro-unit pattern, you multiply the currency value by a large factor—like 100,000—so you only perform arithmetic on whole numbers. This pushes any unavoidable rounding errors from division operations so far down the decimal chain that they become completely irrelevant.

Your first thought might be to just store pence or cents. So, instead of 1.29, you store 129. That works perfectly for basic addition and subtraction. However, as soon as you need to divide a bill or calculate a percentage, you get rounding errors on the penny.

Instead, I recommend scaling the value up further into micro-units. For example, you represent £1.29 as 129,000. Adding and subtracting remains pure, fast integer math. If you eventually hit a rounding error during a complex division step, that error happens at such a microscopic decimal place that you simply don't care. Everything works, and your users' balances remain accurate.

Frequently Asked Questions

Should I use a standard Decimal library or integers for my project?

If your application doesn't have extreme performance constraints, I recommend sticking to your language's built-in Decimal library for safety and readability. Use integer micro-units if you are optimizing for processing speed or working in a highly distributed system where payload size matters.

How do databases typically store money values?

Most relational databases offer a DECIMAL or NUMERIC column type designed specifically for exact precision. If you use the integer micro-unit pattern in your application, you can simply store those scaled values in a standard BIGINT database column.

What happens to fractional cents when dividing payments?

When splitting a value (like dividing 10 cents three ways), standard accounting practice dictates allocating the base divided amount to all parties, then distributing the remainder penny by penny until the remainder is zero. This ensures no money is ever created or destroyed by rounding.