UnitFormat.js
UnitFormat.js is a number formatter for human readable unit numbers, like 10km, 5GB, 17kHz, 220MW, ... with known metric prefixes.
Usage
The interface of UnitFormat.js is a single function that basically takes the number to be formatted and optionally the base unit, like "m" for meters:
let a = // 1kmlet b = // 20kHzlet c = // 1klet d = // 2cm
Installation
Installing UnitFormat.js is as easy as cloning this repo or use the following command:
npm install unitformat
Available Parameters
The whole package consists of a single function unitFormat
with the following signature
- num: the number to be formatted
- baseUnit: the base unit, like meters, Hertz, Joule, ...
- prefixes: which prefixes should be used
- base: The number base, default is 10, but 2 is also possible for bytes
Prefixes
The prefix parameter is a string list of single-character metric prefixes, like kMGTPE
. For base 10 the following prefixes can be used:
E
: ExaP
: PetaT
: TeraG
: GigaM
: Megak
: Kiloh
: Hectod
: Decic
: Centim
: Milliu
: Micron
: Nanop
: Picof
: Femtoa
: Atto
And for base 2 the following prefixes are possible:
k
: KiloM
: MegaG
: GigaT
: TeraP
: PetaE
: Exa
Using UnitFormat.js with the browser
Testing
If you plan to enhance the library, make sure you add test cases and all the previous tests are passing. You can test the library with
npm test
Copyright and licensing
Copyright (c) 2016, Robert Eisele Dual licensed under the MIT or GPL Version 2 licenses.