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

Locale can flip what the , and . operators mean #23

Open
borgar opened this issue May 24, 2022 · 1 comment
Open

Locale can flip what the , and . operators mean #23

borgar opened this issue May 24, 2022 · 1 comment

Comments

@borgar
Copy link
Owner

borgar commented May 24, 2022

This is the content of a workbook file with both versions of the formula, saved under the German locale:

<sheetData>
  <row>
    <c r="A1" t="str">
      <f>TEXT(0, "#.##0,00")</f>
      <v>0,00</v>
    </c>
  </row>
  <row>
    <c r="A2" t="str">
      <f>TEXT(0, "#,##0.00")</f>
      <v>,000</v>
    </c>
  </row>
</sheetData>

What is needed to support this is an option to parse/treat the separators according to the locale:

numfmt("#.##0,00", { locale: "de", localeParse: true })(0) // 0,00
numfmt("#,##0.00", { locale: "de", localeParse: true })(0) // ,000
numfmt("#.##0,00", { locale: "de", localeParse: false })(0) // ,000
numfmt("#,##0.00", { locale: "de", localeParse: false })(0) // ,000
@borgar
Copy link
Owner Author

borgar commented Oct 21, 2022

I've made an attempt at solving this within the parser (as per example above) and I didn't like it. It added execution time for all patterns as well as too much complexity. A better approach may be something like this:

const pattern = numfmt.delocalize("#.##0,00", { locale: "de" }); // "#,##0.00"
const formatter = numfmt(pattern, { locale: "de" });
const output = formatter(0); // 0,00

As visible in #27 the problem also extends beyond the comma and decimal point: Color names are localized as well which either means providing translation tables for all 8 colors (black blue cyan green magenta red white yellow) for all languages, or an option or callback to provide translations/color names, or just being liberal about this and allowing anything inside [] not identified as "something else" (condition, duration, locale...) as color.

borgar added a commit that referenced this issue May 16, 2024
Dramatic overhaul of the parser and interface inspired by #42, the need to expose types (#38), and locale issues (#23).

### Changes from 2.0:

- Formatters are no longer constructed. Formatting is just a simple call. 
   Fixes: #42

- Types are now exposed for the entire interface.
  Closes #38

- _nbsp_ option is now off by default. A non-breaking space is till used as the grouping separator in the default locale, so "13 203" should not linebreak.

- `formatColor()` now returns `null` if pattern does not define a color. Previously it would default to "black".
   Fixes #40

- _locale_ has been fixed so that a pattern may correctly override a locale. The expected behaviour is to be able to provide a default locale and that formatters override it by modifiers.

- Both _en_US_ and _en-US_ styles are now supported for locale tags.

- Group sizing is now controlled by an option, `{ grouping: [ 3, 3 ] }`
  The formatter had remnants of behaviour from [its ancestor](https://github.com/borgar/ldml-number) which allowed defining group sizing in the format. In Excel, `#,##,##0` and `#,##0` are equivalent but sizing is controlled via locale settings.
  Fixes #48

- `dateToSerial()` no longer passes non-dates through. If it gets incompatible input (such as a number), a `null` will be returned.

- _nativeDate_ option has been removed. This affects two things:
  - `dateFromSerial()` now always returns a date parts array (`[ y, m, d, ... ]`).
  - `parseValue()`/`parseDate()` can no longer return dates.

  If you need the old behaviour then here is a utility function that safely converts the output to a `Date`. 

  ```js
  function toNativeDate (dateArrray) {
    const [ y, m, d, hh, mm, ss ] = dateArrray;
    const dt = new Date(0);
    dt.setUTCFullYear(y, m - 1, d);
    dt.setUTCHours(hh, mm, ss);
    return dt;
  }
  ```
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

No branches or pull requests

1 participant