Registry / devops / unitformat

unitformat

JSON →
library1.1.5jsnpmunverified

UnitFormat.js is a lightweight, dependency-free number formatter that converts raw numbers to human-readable strings with metric suffixes like 10km, 5GB, 17kHz, 220MW. Current stable version is 1.1.5 with a slow release cadence. It supports base-10 and base-2 prefixes, custom unit strings, and configurable suffix lists. Differentiators include zero dependencies, TypeScript type definitions, and a single-function API that is easy to embed in browser or Node.js projects. Unlike similar libraries, it does not handle locale-specific formatting or significant figure rounding.

npm install unitformat
INSTALL
IMPORT
SIG · UNITFORMAT
U
unitformat
devopsjavascriptv1.1.5
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import unitFormat from 'unitformat';
const unitFormat = require('unitformat');
ESM import works, but for CommonJS require the default export is accessed as `unitFormat.default` in bundlers. Use named import or destructure as `const { default: unitFormat } = require('unitformat');`.
default
const unitFormat = require('unitformat');
const { UnitFormat } = require('unitformat');
The package exports a single function as default. There is no named export 'UnitFormat'.
default
import unitFormat from 'unitformat';
import { unitFormat } from 'unitformat';
The default export is a function, not a named export.

Demonstrates basic unit formatting with metric prefixes, custom base, and unit string.

import unitFormat from 'unitformat'; // Basic usage - metric prefixes const a = unitFormat(1000, 'm'); // '1km' const b = unitFormat(20000, 'Hz'); // '20kHz' const c = unitFormat(0.02, 'm'); // '2cm' // Without unit const d = unitFormat(1000); // '1k' // Custom prefixes and base const e = unitFormat(1024, 'B', 'kMGTPE', 2); // '1kB' (base 2) console.log(a, b, c, d, e);
Debug
Known issues
gotchaThe function name is 'unitFormat' (lowercase) not 'UnitFormat' (PascalCase).
fix
Use `import unitFormat from 'unitformat'` or `const unitFormat = require('unitformat')`.
affects: *
gotchaCommonJS require returns the default export as a function; destructuring will not work.
fix
Use `const unitFormat = require('unitformat')` and call `unitFormat(num, unit, prefixes, base)`.
affects: *
gotchaThe function does not handle edge cases like NaN, Infinity, or numbers outside supported range gracefully.
fix
Validate input before calling unitFormat.
affects: *
gotchaThe prefixes parameter is case-sensitive; lowercase 'k' for kilo, uppercase 'M' for Mega.
fix
Use the correct case as documented.
affects: *
Errors
Common errors & fixes
UnitFormat is not defined
Attempting to use `UnitFormat` (PascalCase) after import.
fix
Use `unitFormat` (lowercase) as the imported function.
Cannot find module 'unitformat'
The package is not installed or import path is wrong.
fix
Run `npm install unitformat` and ensure the import statement is correct.
TypeError: unitFormat is not a function
CommonJS require returns an object with default property in some bundlers.
fix
Use `const unitFormat = require('unitformat').default` or configure bundler for ESM interop.
Upgrade
Version history
1.1.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
unitformat — npm install unitformat · libregistry