# cldr-compact-number

> Compact number formatting logic

Latest version **0.4.0** (published 2019-11-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install cldr-compact-number
pnpm add cldr-compact-number
yarn add cldr-compact-number
bun add cldr-compact-number
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2019-11-20 |
| First published | 2018-11-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 58.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | Scott Newcomer |
| Maintainers | snewcomer |
| Keywords | short, number, formatting |

## Links

- npm: https://www.npmjs.com/package/cldr-compact-number
- Repository: https://github.com/snewcomer/cldr-compact-number
- Homepage: https://github.com/snewcomer/cldr-compact-number#readme
- Issues: https://github.com/snewcomer/cldr-compact-number/issues
- npm.io page: https://npm.io/package/cldr-compact-number

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 0.4.0 (latest) — 2019-11-20
- 0.3.1 — 2019-11-19
- 0.3.0 — 2019-03-12
- 0.2.2 — 2019-01-13
- 0.2.1 — 2019-01-13
- 0.2.0 — 2019-01-13
- 0.1.2 — 2019-01-11
- 0.1.1 — 2018-11-26
- 0.1.0 — 2018-11-26
- 0.0.6 — 2018-11-20
- 0.0.5 — 2018-11-15
- 0.0.4 — 2018-11-04
- 0.0.3 — 2018-11-04
- 0.0.2 — 2018-11-04
- 0.0.1 — 2018-11-04

## README

cldr-compact-number
==============================================================================
![Download count all time](https://img.shields.io/npm/dt/cldr-compact-number.svg)
[![npm version](https://badge.fury.io/js/cldr-compact-number.svg)](http://badge.fury.io/js/cldr-compact-number)

[![Dependency Status](https://david-dm.org/snewcomer/cldr-compact-number.svg)](https://david-dm.org/snewcomer/cldr-compact-number)
[![devDependency Status](https://david-dm.org/snewcomer/cldr-compact-number/dev-status.svg)](https://david-dm.org/snewcomer/cldr-compact-number#info=devDependencies)

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](https://github.com/unicode-cldr/cldr-numbers-full). Here is the related proposal for [Compact Decimal Format](https://github.com/tc39/ecma402/issues/37) 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](http://www.loc.gov/standards/iso639-2/php/code_list.php).  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.

```js
import compactFormat from 'cldr-compact-number';

compactFormat(19634, 'en', localeData);
// 19K
```

```js
compactFormat(19634, 'en', localeData, {
  significantDigits: 1,
  minimumFractionDigits: 1,
  maximumFractionDigits: 2
});
// 19.6K
```

```js
compactFormat(101, 'en', localeData, {
  significantDigits: 1,
  financialFormat: true
});
// 0.1M
```

```js
compactFormat(19634, 'ja', localeData);
// 2万
```

```js
compactFormat(19634, 'es', localeData, { significantDigits: 1 });
// 19,6 mil
```

* Note when using significantDigits, this addon utilizes [`toLocaleString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/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!!!! (╯°□°）╯︵ ┻━┻
```js
compactFormat(101000, 'en', localeData, { long: true });
// 101 thousand
```

But this does! ʘ‿ʘ
```js
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 git@github.com: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](LICENSE.md).

---
_Source: https://npm.io/package/cldr-compact-number · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
