Javascript Number Formatter
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
-
TypeError: (0 , number_format_js_1.format) is not a function
cause Attempting to import the default `format` function as a named import in an ESM environment.fixChange the import statement from `import { format } from 'number-format.js';` to `import format from 'number-format.js';` -
Output '1234567,890' when expecting '1,234,567' with mask '#,###'
cause The library defaults a single non-digit symbol in the mask to be the decimal separator.fixTo force the symbol as a thousands separator, add a trailing non-mask symbol (e.g., a period) to define the decimal: `format('#,###.', 1234567.890)` to get `1,234,567`. -
Prefix or suffix characters (e.g., 'USD ') are unexpectedly stripped or not appearing in the output.
cause The prefix/suffix contains characters (numbers, dashes, plus signs) that the formatter is explicitly designed to exclude when used as general prefix/suffix symbols, or it's incorrectly positioned in the mask.fixEnsure that literal prefixes/suffixes outside the core numerical pattern do not contain `0-9`, `-`, or `+`. For complex cases, apply the prefix/suffix as string concatenation *after* the `format` call: `'$' + format('#,##0.00', 123.45)`.
Warnings
- breaking Version 2.0.0 introduced the `enforceMaskSign` option, which significantly alters how positive and negative signs are handled within the mask. Code relying on previous sign display behavior, especially for negative numbers or explicit positive signs, may need adjustment.
- gotcha When a mask contains only a single non-digit symbol (e.g., `#,###`), the library will always interpret this symbol as the decimal separator by default, even if it logically should be a thousands separator. This can lead to unexpected output where numbers like `1234567.890` formatted with `#,###` become `1234567,890`.
- gotcha While the library supports prefixes and suffixes within the mask (e.g., `'$ #,###.00'`), the README states that a prefix or suffix *cannot* include any numbers (`0-9`), dashes (`-`), or plus signs (`+`). This is somewhat ambiguous as mask examples show `$` working, which implies this limitation primarily applies to non-mask symbols 'outside' the numerical pattern.
- gotcha This library is a dedicated number formatter with specific limitations. It does not support scientific/engineering notation, date formatting, or phone number formatting. Attempting to use it for these purposes will yield incorrect results.
Install
-
npm install number-format.js -
yarn add number-format.js -
pnpm add number-format.js
Imports
- format
import { format } from 'number-format.js';import format from 'number-format.js';
- format
const { format } = require('number-format.js');const format = require('number-format.js');
Quickstart
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"