Giter Site home page Giter Site logo

cldr-compact-number's Introduction

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

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.

cldr-compact-number's People

Contributors

snewcomer avatar serabe avatar javiergarzac avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.