wNumb: JavaScript Number & Money Formatting
wNumb is a lightweight JavaScript utility designed specifically for number and money formatting. It enables developers to control aspects such as decimal places, thousands separators, and the addition of prefixes or suffixes to numerical values, facilitating clear and readable data presentation. The current stable version is 1.2.0, released in October 2019. The project appears to have an irregular release cadence, with no updates since 2019, suggesting it is currently in a maintenance phase rather than active development. Its core value lies in offering a focused, minimal API for common formatting needs, differentiating itself from more comprehensive internationalization libraries by prioritizing simplicity and a small footprint.
Common errors
-
Formatter outputs '-0' for negative numbers with `decimals: 0`
cause You are using an older version of wNumb (prior to 1.0.2) which had a bug in rounding very small negative numbers.fixUpgrade your wNumb package to version 1.0.2 or higher. -
Unexpected behavior or incorrect parsing when handling decimal values.
cause Your wNumb version is older than 1.0.1, which contained a bug related to decimal processing.fixUpgrade your wNumb package to version 1.0.1 or higher. -
The `postfix` option is ignored or does not apply the desired text.
cause In version 1.1.0, the `postfix` option was renamed to `suffix`. While an internal remapping exists, direct use of `suffix` is recommended.fixChange your formatter configuration from `postfix: 'TEXT'` to `suffix: 'TEXT'`.
Warnings
- breaking The `postfix` option was renamed to `suffix` in version 1.1.0. While `postfix` is remapped internally for backward compatibility, relying on the old name is deprecated and may lead to unexpected behavior or removal in future updates.
- gotcha When formatting small negative numbers with `decimals` set to 0, older versions (prior to 1.0.2) could display '-0' instead of '0'.
- gotcha Early versions (prior to 1.0.1) had issues with decimal handling, potentially leading to incorrect formatting or parsing of values with fractional components.
Install
-
npm install wnumb -
yarn add wnumb -
pnpm add wnumb
Imports
- wNumb
import { wNumb } from 'wnumb';import wNumb from 'wnumb';
- wNumb (CommonJS)
const wNumb = require('wnumb'); - wNumb (Global)
const formatter = wNumb({...}); // Assumes wNumb script tag is present
Quickstart
import wNumb from 'wnumb'; // For module environments like Node.js or bundlers
// If using directly in browser via script tag, 'wNumb' is global.
// Create a currency formatter for USD
const usdFormatter = wNumb({
decimals: 2, // Number of decimals (e.g., 2 for $XX.XX)
mark: '.', // Decimal separator
thousand: ',', // Thousands separator
prefix: '$ ',
suffix: ' USD'
});
console.log(usdFormatter.to(12345.678));
// Expected output: "$ 12,345.68 USD"
console.log(usdFormatter.from('$ 5,432.10 USD'));
// Expected output: 5432.1
// Create a percentage formatter
const percentFormatter = wNumb({
decimals: 0,
suffix: '%'
});
console.log(percentFormatter.to(0.75));
// Expected output: "75%"
console.log(percentFormatter.from('99%'));
// Expected output: 0.99
// A simple number formatter with specific rounding
const numberFormatter = wNumb({
decimals: 3,
encoder: value => value * 2, // Example: custom encode function
decoder: value => value / 2 // Example: custom decode function
});
console.log(numberFormatter.to(10.1234));
// Expected output: "20.247" (10.1234 * 2 = 20.2468, rounded to 3 decimals)