Skip to content

How to fix decimals arithmetic in JavaScript

Find out how to fix decimals arithmetic in JavaScript

If you try to do the sum of two decimal numbers in JavaScript you might have a surprise.

0.1 + 0.1 is, as you expect, 0.2

But sometimes you have some unexpected result.

Like for 0.1 + 0.2.

The result is not 0.3 as you’d expect, but it’s 0.30000000000000004.

Or 1.4 - 1, the result is 0.3999999999999999

How to fix decimals arithmetic in JavaScript

I’m sure your question is: WHY?

First, this is not unique to JavaScript. It’s the same for every programming language.

The reason is due to the fact computers store data as binary, 0 or 1.

Any value is represented in the binary numeric system, as a power of two.

1 is 1 * 2^0

10 is 1 * 2^1 + 0 * 2^0

Not every decimal number can be represented perfectly in this binary format, because some numbers are repeating numbers in binary. Try to convert 0.1 from decimal to binary.

Long story short, we’d need infinite precision to represent 0.1, and while computers can approximate that well, when we do calculations we lose some data since we need to “cut” somewhere, and this leads to those unexpected results you see above.

You can use libraries like decimal.js, bignumber.js or big.js.

You can also use a “trick” like this.

You decide to cut decimals after 2 positions, for example, and multiply the number by 100 to remove the decimal part.

Then you divide by 100 after you’ve done the sum:

0.1 + 0.2 //0.30000000000000004

(0.1.toFixed(2) * 100 + 0.2.toFixed(2) * 100) / 100 //0.3

Use 10000 instead of 100 to keep 4 decimal positions.

More abstracted:

const sum = (a, b, positions) => {
  const factor = Math.pow(10, positions)
  return (a.toFixed(positions) * factor + b.toFixed(positions) * factor) / factor
}

sum(0.1, 0.2, 4) //0.3

→ Get my JavaScript Beginner's Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about js: