Javascript’s Unruly Fractions


I just stumbled across a new factoid regarding javascript. Occasionally JS will throw unneeded decimal places on to float multiplication. For example: 

0.1 * 0.2  // will show 0.020000000000000004

This can be remedied by a couple libraries such as BigDecimal and BigNumber. If you aren’t too concerned with exact fractions then it’s probably easier to use a Math.round

Categorized as news