UnitFormat.js is published as unitformat. Its single function scales a number and appends a selected decimal or binary prefix, producing compact values such as 1.3km, 1.5kB, or -1.5MW.
Use it for compact engineering labels, telemetry, file sizes, and chart axes when a fixed three-decimal rounding policy is acceptable.
<script type="module"> import UnitFormat from 'https://cdn.jsdelivr.net/npm/unitformat@1.1.5/dist/unitformat.mjs'; const formatted = UnitFormat(10000);</script>
The package has no runtime dependencies and supports Node.js 20 or newer.
CommonJS consumers can use the direct function export as well as its .default
and .UnitFormat aliases. These compatibility paths are covered by the test
suite and are part of the supported API.
The interface is a single function that takes the number to be formatted and
optionally the base unit, such as m for meters:
Results are strings rounded to at most three decimal places. Decimal digits, separators, and signs follow the runtime locale when locale formatting is available; otherwise a locale-neutral representation is used.
Enable small metric prefixes explicitly
The third argument is the exact set of allowed prefixes. Include c, m, u, or μ when values below one base unit should be scaled.
baseUnit: the base unit, like meters, Hertz, Joule, ...
prefixes: allowed single-character prefixes; the default is kMGTPE
base: scaling base, either 10 (default) or 2
Suffixes
The suffix parameter is a string list of single-character metric prefixes, like kMGTPE. For base 10 the following prefixes can be used:
E: Exa
P: Peta
T: Tera
G: Giga
M: Mega
k: Kilo
h: Hecto
d: Deci
c: Centi
m: Milli
u: Micro
n: Nano
p: Pico
f: Femto
a: Atto
And for base 2 the following prefixes are possible:
k: Kilo
M: Mega
G: Giga
T: Tera
P: Peta
E: Exa
Building the library
The implementation is written in strict TypeScript. The build emits CommonJS,
ES modules, a standalone browser bundle, source maps, and format-specific type
declarations without modifying source or documentation files.
After cloning the Git repository, run:
npm installnpm run build
Run a test
Testing the source against the shipped test suite is as easy as
npm run test
Copyright and Licensing
Copyright (c) 2026, Robert Eisele
Licensed under the MIT license.