UnitFormat.js is a number formatter for human readable unit numbers, like 10km, 5GB, 17kHz, 220MW, ... with known metric prefixes.
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
Installing UnitFormat.js is as easy as cloning this repo or use the following command:
npm install unitformat
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
The prefix parameter is a string list of single-character metric prefixes, like
kMGTPE. For base 10 the following prefixes can be used:
And for base 2 the following prefixes are possible:
Using UnitFormat.js with the browser
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
Copyright and licensing
Copyright (c) 2016, Robert Eisele Dual licensed under the MIT or GPL Version 2 licenses.