javascript - math

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

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


using Number.prototype.toFixed() - / 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? - - 2015

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


ericelliott/moneysafe :

results for ""

    No results matching ""