If you are going to work with numbers as decimal numbers, use a decimal library like big.js.
Floating point values ββin most languages ββ(including javascript) are stored in binary representation. Basically, it does what you expect. In such cases, your 4.015 is converted to a binary string and, as it turns out, gets the encoding as the value 4.014999999 ..., which you saw is the closest binary representation available in the IEEE754 value with double precision (8 bytes).
If you are doing financial or math for human consumption (i.e. as decimal numbers), you will need to round 4.015 to 4.02, and you will need a decimal library.
It is planned to include the decimal representation of floating point values ββin javascript (e.g. here ), as the new IEEE754-2008 standard includes decimal32, etc. as a decimal representation of floating point values. Read more here: http://speleotrove.com/decimal/
Finally, if you perform mathematical calculations in javascript (for example, financial calculations that should not accidentally create or disappear money), please do all calculations in whole cents / pence.
source share