openexchangerates / accounting.js

A lightweight JavaScript library for number, money and currency formatting - fully localisable, zero dependencies.
http://openexchangerates.github.io/accounting.js
MIT License
4.95k stars 528 forks source link

issue with formatNumber #166

Open pegasus45 opened 7 years ago

pegasus45 commented 7 years ago

Hi,

formatNumber(2.70%0.05,2) is returning undefined instead of 0.

could you help to check? This was working in prior release. Thank you.

gustavlrsn commented 7 years ago

@pegasus45 nice catch! The problem is that 2.70%0.05 evaluates to the number 2.7755575615628914e-17 (should be 0 btw, but that's a floating point error in JavaScript) that uses scientific notation. And the toFixed method was actually using scientific notation to avoid these floating point errors, but did not expect an incoming scientific notation.

I updated the method in #165 and this is now fixed, you could copy the code from there until there is a merge.