number-abbreviate
Abbreviate a number and add unit letters e.g. 2200000 => '2.2m'
Code inspired by http://stackoverflow.com/questions/2685911/is-there-a-way-to-round-numbers-into-a-reader-friendly-format-e-g-1-1k
Example
Simple/shorthand mode:
var abbreviate = require('number-abbreviate')
abbreviate(1000)
abbreviate(1111, 0)
abbreviate(1111, 1)
abbreviate(1111, 2)
Class/constructor mode:
var NumAbbr = require('number-abbreviate')
var numAbbr = new NumAbbr()
numAbbr.abbreviate(12, 1)
numAbbr.abbreviate(0, 2)
numAbbr.abbreviate(1234, 0)
numAbbr.abbreviate(34567, 2)
numAbbr.abbreviate(918395, 1)
numAbbr.abbreviate(2134124, 2)
numAbbr.abbreviate(47475782130, 2)
numAbbr.abbreviate(-1234, 0)
numAbbr.abbreviate(-918395, 1)
numAbbr.abbreviate(-47475782130, 2)
Usage
var NumAbbr = require('number-abbreviate')
var numAbbr = new NumAbbr([units])
numAbbr.abbreviate([number], [decPlaces])
The units
is optional. It can be an array of units, defaulting to ['k', 'm', 'b', 't']
.
decPlaces
is also optional. It defaults to 0.
numAbbr.abbreviate([number], [decPlaces])
Takes a number and the number of decimal places to abbreviate
Installation
npm install number-abbreviate --save
bower install number-abbreviate
Credits
Dom Harrington
http://stackoverflow.com/questions/2685911/is-there-a-way-to-round-numbers-into-a-reader-friendly-format-e-g-1-1k
License
Licensed under the New BSD License