Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

How to solve JavaScript precision loss issue using Decimal.js

It’s not a bug. It’s a feature.

3 min readOct 30, 2025

--

Press enter or click to view image in full size
Hand smashing glass after finding out about precision loss issue
How you might feel when you encounter this

If you’ve ever built a shopping cart in JavaScript, you might have noticed something odd. Your total price sometimes looks slightly off.
At first the total amount is $60, then you remove an item worth $0.10, and suddenly your total becomes $59.89999999999999 instead of $59.9.

Say hello to JavaScript’s floating-point precision loss.

The problem: floating-point math in JavaScript

JavaScript uses 64-bit floating-point numbers (IEEE 754 standard) to represent all numeric values. This means certain decimal numbers, like 0.1 or 0.2, can’t be represented exactly in binary form.

Press enter or click to view image in full size
Kevin Hart
That face when 0.1 + 0.2 ≠ 0.3

This tiny difference might seem harmless…until you’re recalculating totals in a shopping carts, payment systems, invoice apps, or any sort of scientific data that needs absolute precision.

The real-world example: shopping cart total price recalculation

Let’s go back to the shopping cart example.
Imagine a cart where users can add or remove items. Let’s say our cart total is calculated in JavaScript like this:

let total = 60;
const itemPrice = 0.1;

total -= itemPrice;
console.log(total); // 59.89999999999999

When you remove an item worth $0.10, the result is no longer clean. Remove multiple items and you will end up with even bigger difference.

It’s not a bug in your code, it’s just how floating-point math works under the hood.

But in a real application, these kind of errors can have real-world consequences.

To the rescue: Decimal.js

Decimal.js is a small but powerful library that helps you perform arbitrary-precision decimal arithmetic.

Or in simple terms, it stores numbers as real decimals, not binary floats.

Get Nemanja Blagojevic’s stories in your inbox

Join Medium for free to get updates from this writer.

Go ahead and install it:

npm install decimal.js

In action: how Decimal.js works?

Decimal.js wraps numbers in a Decimal object that maintains exact decimal precision.

All arithmetic operations like addition, subtraction, multiplication, and division are done using its methods instead of native operators.

Here’s the same cart example using Decimal.js:

import Decimal from 'decimal.js';

let total = new Decimal(60);
const itemPrice = new Decimal(0.1);
total = total.minus(itemPrice);
console.log(total.toString()); // 59.9

Clean, precise, and exactly what you expect.

You can even set global precision or rounding behavior:

Decimal.set({ precision: 10, rounding: Decimal.ROUND_HALF_UP });

When to use Decimal.js

Use it whenever your app deals with money, measurements, or scientific data, anywhere precision matters.
If your calculations are purely integers or not financially sensitive, you can stick with regular numbers.

Conclusion

Never let your shopping cart total become 59.89999999999999 again.
Use Decimal.js !

A boy doing double finger guns

Thanks for reading!

I am a JavaScript developer writing about interesting encounters along the way.

For more stories just like this, make sure to follow me on Medium 📖.
If you are into coding and side projects, check out my GitHub 🐙.

A message from our Founder

Hey, Sunil here. I wanted to take a moment to thank you for reading until the end and for being a part of this community.

Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? We don’t receive any funding, we do this to support the community. ❤️

If you want to show some love, please take a moment to follow me on LinkedIn, TikTok, Instagram. You can also subscribe to our weekly newsletter.

And before you go, don’t forget to clap and follow the writer️!

--

Nemanja Blagojevic
Nemanja Blagojevic

Written by Nemanja Blagojevic

JavaScript stories along the way.

Stackademic
Stackademic

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.