Javascript Number Formatter

2.0.9 · active · verified Sun Apr 19

number-format.js is a lightweight and performant JavaScript library designed for formatting numerical values using mask patterns. Currently at stable version 2.0.9, it focuses on providing a small footprint (under 2KB production size) and fast execution. It supports a wide array of masking symbols for forced and optional digits, positive/negative signs, and flexible thousands/decimal separators, accommodating various international numbering conventions. A key feature is its ability to automatically round numbers. Unlike more comprehensive internationalization libraries such as `Intl.NumberFormat`, `number-format.js` intentionally limits its scope, specifically not handling scientific/engineering notation, dates, or phone numbers. Its primary differentiator is its simplicity and speed for number mask formatting, making it suitable for scenarios where bundle size and performance are critical.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic number formatting, currency handling, the `enforceMaskSign` option, and custom separators.

import format from 'number-format.js';

// Basic number formatting
console.log(`Formatted 1234567.890 with '#,##0.####': ${format("#,##0.####", 1234567.890)}`);
// Expected: "1,234,567.89"

// Currency formatting with prefix
console.log(`Formatted -1234567.890 with '$ #,###.00': ${format("$ #,###.00", -1234567.890)}`);
// Expected: "$ -1,234,567.89"

// Using enforceMaskSign option (added in v2.0.0)
console.log(`Formatted -1234567.890 with '$ #,###.00' and enforceMaskSign: ${format("$ #,###.00", -1234567.890, { enforceMaskSign: true })}`);
// Expected: "$ 1,234,567.89"

// Forcing positive sign
console.log(`Formatted 1234567.890 with '$ +#,###.00' and enforceMaskSign: ${format("$ +#,###.00", 1234567.890, { enforceMaskSign: true })}`);
// Expected: "$ +1,234,567.89"

// Custom decimal and thousands separators
console.log(`Formatted 98765.432 with '#.##0,00': ${format("#.##0,00", 98765.432)}`);
// Expected: "98.765,43"

view raw JSON →