npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cldr-compact-number

v0.4.0

Published

Compact number formatting logic

Downloads

33,088

Readme

cldr-compact-number

Download count all time npm version

Dependency Status devDependency Status

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.

  • 1234 is converted to 1K in English
  • 101234 is converted to 101K in English and 101.1K if need 1 significant digit
  • 1234 is converted to 1 mil in Español
  • 101234 is converted to 101,1 mil in Español if need 1 significant digit
  • 1234 is converted to 1234 in Japanese
  • 101234 is converted to 10.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 --save

Usage

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);
// 19K
compactFormat(19634, 'en', localeData, {
  significantDigits: 1,
  minimumFractionDigits: 1,
  maximumFractionDigits: 2
});
// 19.6K
compactFormat(101, 'en', localeData, {
  significantDigits: 1,
  financialFormat: true
});
// 0.1M
compactFormat(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 thousand

But 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:

[adlm, ahom, arab, arabext, armn, armnlow, bali, beng, bhks, brah, cakm, cham, cyrl, deva, ethi, fullwide, geor, grek, greklow, gujr, guru, hanidays, hanidec, hans, hansfin, hant, hantfin, hebr, hmng, java, jpan, jpanfin, kali, khmr, knda, lana, lanatham, laoo, latn, lepc, limb, mathbold, mathdbl, mathmono, mathsanb, mathsans, mlym, modi, mong, mroo, ...]

Contributing

Installation

  • git clone [email protected]:snewcomer/cldr-compact-number.git
  • cd cldr-compact-number
  • npm install

Running tests

  • npm run test – Runs the test suite on the current Ember version

License

This project is licensed under the MIT License.