borgar / numfmt

Full ECMA-376 number and date formatting in JavaScript
40 stars 9 forks source link

Specific format string not working correctly for positive numbers #28

Closed dantronik-bugreporting closed 1 year ago

dantronik-bugreporting commented 1 year ago

I'm sorry for this shameless copy of #27, but the bug fix caused the same issue in reverse (positive numbers get displayed as negative numbers).

Here's a shameless copy of the text of #27 with the stuff adapted.

We have the following number format string in the German version of Excel: [>=100]#.##0;[<=-100]-#.##0;#.##0,00

If we format the number -3,96 with it, the number gets output properly as -3,96.

In order to use the number format string with numfmt, we swap the "," and "." characters (because thousands separator and decimal separator are swapped in German), like so:

"[>=100]#.##0;[<=-100]-#.##0;#.##0,00"
.replaceAll(",", "|")
.replaceAll(".", ",")
.replaceAll("|", ".")
.replaceAll("Rot", "Red")
.replaceAll("ROT", "RED")
.replaceAll("rot", "red")
.replaceAll("JJJJ", "yyyy");

Which gives me '[>=100]#,##0;[<=-100]-#,##0;#,##0.00'

Interestingly enough, the positive number 6 gets formatted as -6.00.

borgar commented 1 year ago

Yes, you are right. I've only managed to move the problem. I've been looking into this and it looks like the conditional patterns have more complex rules around them than I had previously thought. I am in the process of trying to discover how they actually work.

borgar commented 1 year ago

numfmt@2.4.3 is up on npm and includes a fix for this.