6

I'm trying to use the new Javascript internationalization API, and would like to know if there is a way to get the decimal and thousands (grouping) separator for a Intl.NumberFormat instance?

There is a resolvedOptions method on the object, but that does not provide the symbols.

In case anybody's wondering, then for en-US, these would be a comma , and period ., such as in 1,000.00.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
ragulka
  • 4,312
  • 7
  • 48
  • 73
  • Could the `positivePattern` be of use? If nothing else, you could always see what a known number actually resolves to. :) – bzlm Sep 08 '14 at 11:16
  • @bzlm parsing the resolved pattern is an option I thought of. For example, formatting a known number, such as `1000.50` and checking at the character just after number `1` for a grouping (thousand) separator, and the character(s) between the last `0` and just before number `5` for the decimal separator. – ragulka Sep 09 '14 at 08:35
  • That's exactly what I do when nothing else is available (but with 12345.6789). I find it usually works quite well. – bzlm Sep 09 '14 at 10:32
  • Good thinking. Using different digits should make it more fool-proof. – ragulka Sep 09 '14 at 10:36
  • How do you actually get JavaScript to give you a string version of a number in the current locale though? `parseFloat(12345.6789).toString()` just emits 12345.6789... We would be looking for 12,345.6789 right? – bzlm Sep 09 '14 at 10:41
  • [`(12345.6789).toLocaleString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString) – ragulka Sep 10 '14 at 06:52
  • Thanks. I've converted this thread to an answer, to help future viewers of this question. Hopefully the JS I18N API can be use instead soon. – bzlm Sep 10 '14 at 09:23
  • Does this answer your question? [How do I find the decimal separator for current locale in Javascript](https://stackoverflow.com/questions/33159354/how-do-i-find-the-decimal-separator-for-current-locale-in-javascript) – Rubén Jul 12 '20 at 01:02

2 Answers2

2

If nothing else, as a trick solution (that doesn't pass the Turkey Test; see comment), you can use the output of toLocaleString() to determine information about number formatting in a locale. For the current locale, for example:

var decimalSeparator = 
    (12345.6789).toLocaleString().match(/345(.*)67/)[1];
var thousandSeparator = 
    (12345.6789).toLocaleString().match(/12(.*)345/)[1];
var numberOfDecimals = 
    (12345.6789).toLocaleString().match(/345(\D*)(\d+)$/)[2].length;

The same trick can be used for currency formatting, using e.g. (12345.6789).toLocaleString("en-GB", { style: "currency" }).

bzlm
  • 9,626
  • 6
  • 65
  • 92
  • 1
    This is probably similar to the approach I'll end up using, but it's worth noting that this will probably break with locales that don't print out 1234 etc. For example, using 'ar' 1234 = ١٢٣٤ – Andyrooger Oct 21 '15 at 22:18
1

I'm afraid ECMA-402 standard does not define the API that let you access separators. There is also another problem - at the moment the adoption of ECMA-402 is not as wide as we wish.

Therefore for the time being, if I were you I would look to something like CLDR JSON bindings or iLib which apparently provides these information through LocaleInfo's getDecimalSeparator() and getGroupingSeparator() functions.
BTW. iLib seems to use CLDR as a source of information.

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79