globalizejs / globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
https://globalizejs.com
MIT License
4.8k stars 603 forks source link

No parsing support for number compact option #800

Closed Kirill486 closed 6 years ago

Kirill486 commented 6 years ago

Hi, guys) I spent some time trying to get my number formatted as thousands and millions and billions and so on. This wasn't that much an issue in previous version (0.x.x). I'm about to give up. Could you possibly somehow guide me out? After I faced a failure with the 'compact' option, I went to see number putterns on http://cldr.unicode.org/translation/number-patterns#TOC-Pattern-Characters They don't work either. I tried: Globalize('en').formatNumber(100000, {style: 'decimal'})
"100,000"
Globalize('en').formatNumber(100000, {raw: '00K'})
"100000K"
Globalize('en').formatNumber(100000, {raw: '#,##0%'})
"10,000,000%"
Globalize('en').formatNumber(100000, {compact: 'short'})
"100,000"
Globalize('en').formatNumber(100000, {compact: 'long'})
"100,000"
Globalize('en').formatNumber(1, {style: 'percent'})
"100%"
This is the only one that work correctly.

What i need is: format my numbers using styles listed in 'decimalFormats' section of the number.json Is that possible?

rxaviers commented 6 years ago

Please, which globalize version are you using?

Kirill486 commented 6 years ago

I use v 1.1.1

rxaviers commented 6 years ago

Please, test it using 1.4.0-alpha.0?

rxaviers commented 6 years ago

1.4.0-alpha.0 is the version where you will find compact support.

Kirill486 commented 6 years ago

Even in newer version this functionality seem not working correctly. var formatteden = Globalize('en').numberFormatter({compact: "long"})(100000) undefined formatteden "100 thousand" var parceden = Globalize('en').numberParser({compact: "long"})(formatteden) undefined parceden NaN

And is there a way specify the format (thousands or millions or billions)?

rxaviers commented 6 years ago

Thanks for checking, reopening then...

Kirill486 commented 6 years ago

I came to conclusion, that normally you wouldn't parse compact numbers(it has a hudge unaccuracy, even billions is an option), it's just worth mentioning in documentation.

rxaviers commented 6 years ago

Clarifying this issue... Formatting works fine [1], but there's no parsing support for number compact option.

1:

Globalize('en').numberFormatter({compact: "long"})(100000)
// > '100 thousand'