#### Don’t we all love Javascript?

The most annoying thing when you have to do some math operation in Javascript is that sometimes it does not work the way you want.

Take a look at this example

0.1 + 0.2 //0.30000000000000004

#### What is going on here?

This is not Javascript acting weird but it’s just the nature of how computers work. Computers internally use binary floating point to denote decimal. Unfortunately, it cannot accurately represent numbers like 0.1, 0.2 or 0.3 at all.

Try any programming language without Decimal Datatype . This is a common issue.

#### Solution

There are several approaches one can take to tackle this problem. Using a library such as https://github.com/MikeMcl/decimal.js/ is a pretty decent choice. However, I only want to do a simple math operation (+ , — , x, %) and I think using that lib is a bit overkill.

I decided to write my own preciseMath.js and here is how it looks

The code does a simple rounding with a nice little touch that you could also specify decimal digits precision as the last argument. The default is 12 digits.

import { preciseAdd } from 'preciseMath'