@ionaru/format-number
v5.0.0
Published
A sturdy and effective number formatter.
Downloads
274
Readme
@ionaru/format-number
Description
A package for formatting a number to improve its readability.
Usage
npm install @ionaru/format-number
Standard use
import { formatNumber } from 'format-number';
const formattedNumber = formatNumber(500.8);
console.log(formattedNumber); // 500.80
No decimals
import { formatNumber } from 'format-number';
const formattedNumber = formatNumber(1.62, 0);
console.log(formattedNumber); // 2
API
formatNumber(amount, decimalAmount, decimalMark, delimiter) ⇒ string
Format a number to make it more readable.
Kind: global function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| amount | number | string | | The amount that will be formatted, must be convertible by Number()
. |
| decimalAmount | number | 2 | The amount of decimal places will be in the formatted number. |
| decimalMark | string | "." | The character that marks the start the fractional part of the formatted number. |
| delimiter | string | "," | The character that separates the thousands in a number. |
Examples
Standard formatting is great for monetary values.
const formattedNumber = formatNumber(19499.99);
console.log(formattedNumber); // 19,499.99
Show only whole numbers
const formattedNumber = formatNumber(19499.99, 0);
console.log(formattedNumber); // 19,500
The input number can be a string
const formattedNumber = formatNumber('19499.99');
console.log(formattedNumber); // 19,499.99
Change the default markings in the number for international use.
Canadian
const formattedNumber = formatNumber(19499.99, undefined, ',', ' ');
console.log(formattedNumber); // 19 499,99
Dutch
const formattedNumber = formatNumber(19499.99, undefined, ',', '.');
console.log(formattedNumber); // 19.499,99