Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unformat with the decimal specified to deal with decimal for thousands #183

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

pycarlson
Copy link

Signed-off-by: Brad Midgley [email protected]

formatMoney will fail on currencies that have a dot for the thousands separator.

fixes: #129

@thomasjlee
Copy link

After looking into this issue it is apparent that the intended usage of formatMoney does not securely allow for number to be passed as a string. Numbers which use "." as a thousand separator must be wrapped in quotes, since JavaScript will not correctly interpret a number such as 1.234,56. The proposed fix may not be reliable since options to formatMoney can be passed either individually:

formatMoney( number, "R$", 2, ".", ",", "%s %v" ); // 6 arguments

... or, as an object:

formatMoney( number, {
  symbol: "R$",
  precision: 2,
  thousand: ".",
  decimal: ",",
  format: "%s %v"
} ); // 2 arguments

I believe the best work around for currencies which use ".' as a thousand separator is this two step process:

// Manually unformat the number passed as a string:
var number = accounting.unformat( "1.234,56", "," ); // number => 1234.56

// Pass the unformatted number to formatMoney with appropriate options:
accounting.formatMoney(number, {
  symbol: "R$",
  thousand: ".",
  decimal: ",",
  format: "%s %v"
}); // "R$ 1.234,56"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

formatNumber does not support custom decimal?
2 participants