Number-js-formatter is a JavaScript library that formats numbers in a human-friendly way.
Perhaps a number like 1000000 is coming from an API you're using or you just want to display a number in a more friendly format; number-js-formatter helps you do that with ease.
- Editor: https://stackblitz.com/edit/react-7vx6bf?file=src/App.js
- App URL: https://react-7vx6bf.stackblitz.io
Make sure you have the following installed:
- Node.js(v14+ or latest LTS)
npm install number-js-formatter
# or
yarn add number-js-formatter
# or
pnpm add number-js-formatter
This library is minimal and doesn't require any configuration.
import { friendlyFormat } from 'number-js-formatter';
const num1 = 1_923_500.156;
const num2 = 1_004_850_000.91332;
const num3 = 1105_830_000_302.9946;
friendlyFormat(num1, { noOfDigitsAfterDecimal: 3 }); // "1.924m"
friendlyFormat(num2, { noOfDigitsAfterDecimal: 4, form: '-ln' }); // "1.0049bln"
friendlyFormat(num3, { noOfDigitsAfterDecimal: 3 }); // "1.106tn"
Method | Description |
---|---|
friendlyFormat |
Formats a number into a human-friendly number. |
Method | Args | Description |
---|---|---|
friendlyFormat |
number: number |
number to be formatted |
FormatOptions object |
parameters to use for formatting |
- Format options in different short forms.
- Strongly typed; number-js-formatter is purely written in TypeScript.
- Doesn't support quadrillions numbers
- Locale support coming soon
Read through our contributing guidelines to learn about our submission process, coding rules, and more.