javascript-number-formatter icon indicating copy to clipboard operation
javascript-number-formatter copied to clipboard

Negative numbers with currency symbol: incorrect format

Open maxkan opened this issue 8 years ago • 3 comments

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

maxkan avatar Sep 19 '17 15:09 maxkan

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 avatar Sep 19 '17 22:09 Mottie

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

Mottie avatar Sep 19 '17 22:09 Mottie

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.

maxkan avatar Sep 20 '17 02:09 maxkan