Mottie / javascript-number-formatter

Lightweight & Fast JavaScript Number Formatter
http://mottie.github.io/javascript-number-formatter/
MIT License
141 stars 37 forks source link

Negative numbers with currency symbol: incorrect format #7

Open maxkan opened 7 years ago

maxkan commented 7 years ago

format('$#,##0.', -100) should return '-$100', the same way as Excel does it

Mottie commented 7 years ago

Hi @maxkan!

Hmm, adding this ability to the formatter would make it much more complicated. There are many currency symbols and the mask could also include a longer prefix (e.g. The amount was € #,##0.)

Maybe adding a placeholder to the mask for the positive and/or negative sign would work better. Something like this:

format("$#,##0.", 100); // 0
format("$#,##0.", -100); // $-100
format("{-}$#,##0.", 100); // 0
format("{-}$#,##0.", -100); // -0
format("{+-}$#,##0.", 100); // +0

What do you think?

Mottie commented 7 years ago

And I actually thought Excel wrapped negative numbers in parenthesis (e.g. (12.34) = -$12.34). Did that change?

maxkan commented 7 years ago

It's customizable in Excel. As a variant it can be done using separate methods like formatCurrency. But a lot of people is familiar with Excel custom cell format and as I see it the closer this lib to Excel formatting the more useful it is because you can just use the format you know and receive the result you expect.... As for now I'm checking if the value is negative and adding '-' manually.