# JavaScript Math

# float and money

JavaScript Numbers are IEEE 754 64-bit floating point. The result is that we can't safely add money because the decimal will get skewered by floating point rounding errors.

.2 + .1 === .3; // false
1

# the money problem

Floating Point Numbers & Currency Rounding Errors - Patrick Bacon - 20140814

Don’t use floating point numbers for money

Ways it Can Go Wrong :

  • During Calculations
  • During Storage
  • During Retrieval
  • Before Display

stackoverflow comment explaining the problem

# quicks solutions

# toFixed()

using Number.prototype.toFixed() - stackoverflow.com / MDN

Warning toFixed returns a string. This to prepare display in UI not to do maths.

# do calculation on cents

What is the best way to handle floating point problems with financial calculations in JavaScript? - quora.com - 2015

just represent all monetary values using an integer number of the smallest relevant units of the currency

# libs

ericelliott/moneysafe : github.com