Skip to content

Typescript function for formatting SI units, turn 0.001 into "1m

License

Notifications You must be signed in to change notification settings

tscircuit/format-si-unit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

format-si-unit

A lightweight utility to format numbers with SI unit prefixes (k, M, G, etc.).

Installation

npm install format-si-unit
# or
bun add format-si-unit

Usage

import { formatSiUnit } from "format-si-unit"

console.log(formatSiUnit(1500)) // "1.5k"
console.log(formatSiUnit(1_000_000)) // "1M"
console.log(formatSiUnit(0.001)) // "1m"
console.log(formatSiUnit(0)) // "0"
console.log(formatSiUnit()) // ""

Features

  • Supports SI prefixes from pico (p) to tera (T)
  • Handles null and undefined inputs
  • Returns numbers with up to 3 significant digits
  • Automatically selects the most appropriate SI prefix
  • TypeScript support included

Development

To install dependencies:

bun install

To run tests:

bun test

To build:

bun run build

License

MIT

About

Typescript function for formatting SI units, turn 0.001 into "1m

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published