borgar / numfmt

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

Comma operator does not work correctly #48

Closed borgar closed 5 months ago

borgar commented 11 months ago

In numfmt the comma operator is trying to things it shouldn't and is sometimes misidentified as scaling when it should mean grouping:

  1. format('0,,0', 12) will output "12" when it should output "12,345,678".

    Here the formatter is interpreting the commas as "scaling" when it should mean "grouping". My current hypothesis is that if the comma is followed by any numerical digit, then it means "use grouping" (0,,9 groups whereas 0,,x does not).

  2. format('0,0', 12345678) will output "1,2,3,4,5,6,7,8" when it should output "12,345,678".

    The formatter is grouping by a 1 digit where it should be grouping by 3. Loosely investigating this it turns out that the Excel formatter does not support grouping control through syntax. Using locale codes does not affect things: [$-02010439]0,0 (Hindi/Gregorian/Hindi) prints "١٢,٣٤٥,٦٧٨". It does however support alternative digit grouping through system settings.

    Numfmt is originally based on an LDML formatter which does support dictating grouping size via the pattern so that's why it's doing this.

  3. format('0,00', 12345678) will output "12,34,56,78" when it should output "12,345,678".

    This is, again, doing the correct thing for LDML but not what the Excel format wants.

What likely needs to happen is:

  1. Stop using the pattern to determine grouping size and rather expose that as a call-time option.
  2. Consider a comma to mean "scaling" until it is followed by a digit, in which case it clears scaling factor and activates "grouping" instead.