Clarify unformat() usage (Issue #175), and fix bug related to using non-standard separators without changing default settings object (Issue #129). #186
Edited (26 November 2017): There are additional issues related to passing number as strings to formatMoney and formatNumber, so I have closed the pull request for now.
Important: Changes to accounting.js are not reflected in the minified version.
Issues Addressed
#175 - To call unformat() on a number that uses a decimal separator other than "." (eg. a comma), user must pass in the decimal separator as the second argument to unformat().
This is not a bug. However, the current docs do not provide sufficient examples for the unformat method. I have updated the description of unformat() in the "Library Methods" section of the documentation with example code, as well as added QUnit tests to better demonstrate this usage.
#129 - Some users who handle currencies which use "." as thousand separators and "," as decimal separators may prefer to pass numbers as strings into formatMoney() and formatNumber() because JavaScript will not appropriately recognize a float such as 1.234.567,89.
If the default settings object is altered prior to using formatMoney() or formatNumber(), this works just fine:
These issues occur even if options are passed individually, rather than as an object.
When exploring this issue, I noticed a rather simple change would fix it:
unformat() takes two arguments: value, and decimal.
Nowhere in the API methods is unformat used in this way. In both formatMoney() and formatNumber(), unformat() is called without a decimal argument even if it was passed explicitly by the user. (I suspect this was an oversight.)
By first building the options object and then cleaning the number using unformat, passing in both number and decimal separator, ie. number = unformat(number, opts.decimal);, this ensures the unformat method will respect the decimal separator.
Rather than adding a specific check for the usage of the Brazilian currency symbol ("R$") as proposed by PR #183, this subtle change will handle all cases where number is passed as string and non-default separators are used.
Edited (26 November 2017): There are additional issues related to passing number as strings to formatMoney and formatNumber, so I have closed the pull request for now.
Important: Changes to accounting.js are not reflected in the minified version.
Issues Addressed
#175 - To call unformat() on a number that uses a decimal separator other than "." (eg. a comma), user must pass in the decimal separator as the second argument to unformat().
Example:
accounting.unformat("100,00", ",") // 100
This is not a bug. However, the current docs do not provide sufficient examples for the unformat method. I have updated the description of unformat() in the "Library Methods" section of the documentation with example code, as well as added QUnit tests to better demonstrate this usage.
#129 - Some users who handle currencies which use "." as thousand separators and "," as decimal separators may prefer to pass numbers as strings into formatMoney() and formatNumber() because JavaScript will not appropriately recognize a float such as
1.234.567,89
.If the default settings object is altered prior to using formatMoney() or formatNumber(), this works just fine:
However, should a user prefer not to alter the settings object (and rather pass an options object directly), this issue arises:
The same issue arises in formatNumber():
These issues occur even if options are passed individually, rather than as an object.
When exploring this issue, I noticed a rather simple change would fix it:
number = unformat(number, opts.decimal);
, this ensures the unformat method will respect the decimal separator.Rather than adding a specific check for the usage of the Brazilian currency symbol ("R$") as proposed by PR #183, this subtle change will handle all cases where number is passed as string and non-default separators are used.
With the changes:
I have also added additional QUnit and Jasmine tests to demonstrate this minor change works.
Final Considerations