3 ms·
What Every Web Programmer Should Know About Floating Point: 0. Do not calculate monetary amounts in FP. Businesses don't react well to being told about accepta
by jacques_chester 14y ago
What Every Web Programmer Should Know About Floating Point:
0. Do not calculate monetary amounts in FP. Businesses don't react well to being told about acceptable imprecision.
1. Javascript is floating point.
2. Do not calculate money amounts in Javascript.
There are decimal libraries for Javascript (eg [1][2]). Use those if you need to calculate monetary amounts.
[1] https://github.com/whatgoodisaroad/Big-js https://github.com/whatgoodisaroad/Big-js
[2] https://github.com/dtrebbien/BigDecimal.js https://github.com/dtrebbien/BigDecimal.js
- whichdan 14y agoI'm assuming that storing $129.99 as 12999 and dividing by 100 when necessary would be preferable?
- jacques_chester 14y agoThe problem is that every javascript number is floating point. Even if you think you have integers ... sometimes you don't.