3 ms·
accounting.js [0] handles this by multiplying the number out by a few thousand before doing the maths, ensuring rounding works as expected. Source: Gordon Zhu’
by jen729w 9y ago
accounting.js [0] handles this by multiplying the number out by a few thousand before doing the maths, ensuring rounding works as expected.
Source: Gordon Zhu’s excellent introduction to JS course, Watch and Code [1]. I haven’t used accounting.js myself.
0: http://openexchangerates.github.io/accounting.js/ http://openexchangerates.github.io/accounting.js/
1: http://watchandcode.com http://watchandcode.com
- partycoder 9y agoThat is not enough. Converting an amount to cents, e.g: $1.99 becomes 199 cents, will help you only briefly. As numbers become larger or you have successive divisions, numbers will start to suffer from issues such as absorption. Then you may also suffer from issues when comparing numbers, since floating point equality is different. e.g: > 1e16 -1 10000000000000000 Note that this code should output 9999999999999999. But it doesn't because of absorption. In this case, "multiplying by a few thousands" makes this situation more likely.