cldr-compact-number v0.4.0
cldr-compact-number
Compact number formatting based on CLDR locale data. Particularly useful for statistical data, showing financial numbers in charts, and abbreviating number of ratings across a range of languages.
1234is converted to1Kin English101234is converted to101Kin English and101.1Kif need 1 significant digit1234is converted to1 milin Español101234is converted to101,1 milin Español if need 1 significant digit1234is converted to1234in Japanese101234is converted to10.1万in Japanese if need 1 significant digit
Depends on data from cldr-numbers-full. Here is the related proposal for Compact Decimal Format that this addon is based on. This is why there are no browser API's baked into something like Intl.NumberFormat.
Installation
npm install cldr-compact-number --saveUsage
The following APIs take the language code as the the second argument based on ISO 639-1. You can also pass en_GB and we will normalize it to en-GB as well.
API
We default this library with en localeData. What is localeData?
localeData for most cases comes through a build tool. You define what languages you want upfront so as to avoid bloating your application bundle and the build tool parses CLDR data and formats it. See priv/ for examples of what shape the data should be in if you want to manually construct this data.
import compactFormat from 'cldr-compact-number';
compactFormat(19634, 'en', localeData);
// 19KcompactFormat(19634, 'en', localeData, {
significantDigits: 1,
minimumFractionDigits: 1,
maximumFractionDigits: 2
});
// 19.6KcompactFormat(101, 'en', localeData, {
significantDigits: 1,
financialFormat: true
});
// 0.1McompactFormat(19634, 'ja', localeData);
// 2万compactFormat(19634, 'es', localeData, { significantDigits: 1 });
// 19,6 mil- Note when using significantDigits, this addon utilizes
toLocaleString.
Long Formatting
"Wait, I thought this addon was for compact number formatting?" Well it can be a misnomer depending on the language. Let's look at some examples.
This doesn't seem shorter!!!! (╯°□°)╯︵ ┻━┻
compactFormat(101000, 'en', localeData, { long: true });
// 101 thousandBut this does! ʘ‿ʘ
compactFormat(101000, 'ja', localeData, { long: true });
// 101万So we will just go with cldr-compact-number for now.
Other
Currently this only shortens with latin digits 0..9
For your information, known number systems include:
Contributing
Installation
git clone git@github.com:snewcomer/cldr-compact-number.gitcd cldr-compact-numbernpm install
Running tests
npm run test– Runs the test suite on the current Ember version
License
This project is licensed under the MIT License.