wNumb: JavaScript Number & Money Formatting

1.2.0 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to create and use wNumb formatters for currency, percentages, and custom numeric transformations, including encoding and decoding values.

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)

view raw JSON →