NumberSuffix
By Moti Shriki
What?
NumberSuffix is a package for ceiling numbers with a suffix for its zeros. Example:
- 1,234 -> 1k
- 1,234,567 -> 1M
Featues
- Precision
- Fixed measurement
- Adding more formatting styles, already supports:
- Metric (default)
- Abbreviation
- Support for the following units:
- thousand
- million
- billion
- trillion
- quadrillion
- quintillion
number-abbreviate
, numbr
or numeraljs
?
Why not using Mainly because of the libraries usage. I needed something very generic to work with for a project I'm currenly working on. The other libraries had either a specific usage (which works well on the wide internet) or a missing feature that I was needed.
number-abbreviate
was the closest thing I found that meets my expectation - but lacked one feature I needed: a fixed measurement for numbers.
Also, this library uses mainly mathematic function for calculations compared to other libraries that uses strings instead, leading to slower performance.
Installation
Just npm-install it:
npm i number-suffix --save
Usage
Basic usage:
NumberSuffix // 1kNumberSuffix // 1MNumberSuffix // 1GNumberSuffix // 1T
Precision:
NumberSuffix // 1.23kNumberSuffix // 1.23M
Fixed measurement: Supports: 'thousand', 'million', 'billion', 'trillion', 'quadrillion' and 'quintillion'.
NumberSuffix // 1234kNumberSuffix // 0.001M
Abbreviation:
NumberSuffix // 1KNumberSuffix // 1B
And adding and using a new style:
NumberSuffixNumberSuffix // 1Million
If there are null values it will lean on the metric style as a fallback.
Instance
You can create an instance of NumberSuffix for more specific usage without using global settings.
const numberSuffix = ...
You can use fixed options in addition to the ones you have:
const numberSuffix = style: 'abbreviation'numberSuffix // 1K
const numberSuffix = precision: 2numberSuffix // 1.23k
const numberSuffix = measurement: 'thousand'numberSuffix // 1234k
And, of course, to override them for even more specific usage:
const numberSuffix = measurement: 'thousand'numberSuffix // 1M
You can add your own style as well with
const numberSuffix = numberSuffixnumberSuffix
To change the fixed options you can just:
const numberSuffix = numberSuffix