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

Update toFixed to round negative values away from zero and handle exponential notation #165

Closed gustavlrsn closed 1 year ago

gustavlrsn commented 7 years ago

Fixes an inconsistency between toFixed and formatNumber referenced in #164.

Updating toFixed so that it rounds negative values away from zero, to be in consistence with how formatNumber and thus formatMoney has been working.

Also adding some tests to formatNumber to clarify this current (& past) behaviour of rounding negative values away from zero.

Before:

console.log(accounting.toFixed(-61.005, 2)); // "-61.00"
console.log(accounting.formatNumber(-61.005, 2)); // "-61.01"

with this fix:

console.log(accounting.toFixed(-61.005, 2)); // "-61.01"
console.log(accounting.formatNumber(-61.005, 2)); // "-61.01"
gustavlrsn commented 7 years ago

Updated the method to handle values that are represented in exponential notation, and fixing #166. Also refactored the method a little bit with more comments explaining what it does. Also added tests for testing incoming values in exponential/scientific notation.