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.
npm install number-format.jsVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic number formatting, currency handling, the `enforceMaskSign` option, and custom separators.
Review mask patterns involving `+` or `-` symbols and explicitly use the `enforceMaskSign: true` option if signs should be enforced according to the mask rather than the value's inherent sign.
To force a single symbol to be used as a thousands separator, append a trailing non-mask symbol (e.g., a period) to the end of the mask, such as `#,###.` This explicitly defines the last symbol as the decimal, forcing others to be separators.
Avoid using numerical digits, dashes, or plus signs as literal prefix/suffix characters within the mask itself if they are intended as non-formatting text. For complex prefixes/suffixes, consider concatenating them before/after the formatted string: `'$' + format('#,##0.00', 123.45)`.For date formatting, use `Intl.DateTimeFormat` or a dedicated date library like `date-fns`. For phone numbers, use a specialized library like `libphonenumber`. For scientific notation, utilize JavaScript's built-in `Number.prototype.toExponential()` or `Intl.NumberFormat` with `notation: 'scientific'`.
Change the import statement from `import { format } from 'number-format.js';` to `import format from 'number-format.js';`To 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`.Ensure 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)`.No dependency data recorded yet.