About Floating Point Numbers in JavaScript

Don't trust the default float type implementation when working with floating point numbers in JavaScript. For example:

1
2
> 0.1 + 0.1 + 0.1
0.30000000000000004

Instead, do the calculations by using a library like decimal.js.

1
2
3
> import Decimal from 'decimal.js';
> new Decimal('0.1').plus('0.1').plus('0.1').toString()
'0.3'

Tips and Tricks Programming