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.96k stars 530 forks source link

Numbers entered with custom decimal get returned 100x higher (separators placed at wrong places) #56

Open erlingormar opened 11 years ago

erlingormar commented 11 years ago

In using accounting.js, I noticed something strange when using a custom decimal separator (the European-style comma).

The following code sample runs accounting.formatNumber() on two <input>-fields to format their values (which works fine). Clicking a textbox will replace the value with the unformat()-ed number (which works fine). However, when I change the value by typing a value such as 64,00 (comma included) and call formatNumber(), the result will have the decimal/thousand-separators placed in the wrong place (6.400,00).

The number is correctly formatted according to the preferences... only it's 100 times the original value ;)

I have set up an example here: http://jsfiddle.net/erlingormar/qp7mz/12/

gabrielnau commented 11 years ago

I ran into the same problem, it's actually not a bug but this may be more clear in the doc :

// If a non-standard decimal separator was used (eg. a comma) unformat() will need it in order to work out // which part of the number is a decimal/float : accounting.unformat("€ 1.000.000,00", ","); // 1000000

So by having "," as a decimal separator, since unformat is called in formatNumber, you have to do something like this : result = accounting.formatNumber( accounting.unformat( input_value, ",") )