zuji
v1.0.8
Published
A lightweight number formatting utility
Maintainers
Readme
zuji is a developer friendly API for formatting numbers. It is:
- straightforward to pick up - the entire API surface is a single function + a few exported types if you need them
- standards based - extends the
Intl.NumberFormatAPI, making it consistent with JavaScript runtimes - comprehensive - covers virtually every option you need to format numbers, supporting every ISO standard locale
- tiny - zero dependencies and single purpose
- flexible - when you need to configure it further
Installation
npm install zuji
# or
pnpm add zuji
# or
yarn add zuji
# or
bun add zujiUsage
import { zuji } from "zuji";
// use shortcuts
// -> currency
zuji(1234.56, "compact-currency-usd"); // "$1.23K"
// -> integer
zuji(1234, "standard-integer"); // "1,234"
// -> percentage
zuji(0.1234, "compact-percent"); // "12%"
// or fallback to an even narrower typed Intl.NumberFormatOptions
// -> currency without trailing zeros, in accounting notation
zuji(-1050, {
style: "currency",
currency: "USD",
currencySign: "accounting",
trailingZeroDisplay: "stripIfInteger",
}); // "($1,050)"Why zuji?
Number formatting is simple on the surface but becomes increasingly complex when you introduce:
- Different grouping separators (1,000 in US vs 1.000 in Spain)
- Currencies ($ vs ¥)
- Notations (1,000,000 vs 1M vs 1e6)
- Rounding (0.99 -> 100% vs 0.99 -> 99%)
- And more...
zuji gives you a method to cover these problems out of the box. You won't have to learn a new formatting grammar or pour through MDN docs to get it right.
Built-in Shortcuts
The easiest way to format numbers is using pre-configured shortcuts:
standard-decimal- Normal decimal with grouping and no roundingstandard-integer- Number with grouping rounded to integercompact-decimal- Abbreviated number with shortened labelcompact-integer- Abbreviated number rounded to integerstandard-percent- Percentage with grouping and decimalscompact-percent- Percentage without decimalsstandard-currency-usd- US Dollar currencycompact-currency-usd- Abbreviated US Dollar currencyaccounting-currency-usd- US Dollar with accounting notation- And more...
API Reference
zuji takes two arguments:
- value - The number to format
- options - Either a pre-configured string shortcut or a typed
ZujiOptionsobject
The ZujiOptions object supports all Intl.NumberFormat options including:
style- decimal, currency, percent, unitnotation- standard, scientific, engineering, compactunit- kilometer, celsius, megabyte, etc.currency- USD, EUR, JPY, etc.signDisplay- auto, always, never, exceptZeroroundingMode- halfExpand, ceil, floor, etc.- And many more...
See the full documentation for complete examples and API reference.
TypeScript
zuji is written in TypeScript and exports helper types including ZujiShortcut and ZujiOptions. Intellisense and autocomplete with descriptive examples will show up in your editor for any option you provide.
Why the name?
The name zuji comes from the Japanese word sūji (数字), which means "number" or "numeral".
Playground
See the interactive playground to explore the API and test out formatting options.
License
MIT © Kyle Gill
