What is @formatjs/intl-numberformat?
@formatjs/intl-numberformat is a JavaScript library that provides a robust and flexible way to format numbers according to different locales and formatting options. It leverages the ECMAScript Internationalization API (Intl) to offer a wide range of number formatting capabilities, including currency, percentages, and custom number formats.
What are @formatjs/intl-numberformat's main functionalities?
Basic Number Formatting
This feature allows you to format numbers according to a specified locale. In this example, the number 1234567.89 is formatted according to the 'en-US' locale.
const { NumberFormat } = require('@formatjs/intl-numberformat');
const formatter = new NumberFormat('en-US');
console.log(formatter.format(1234567.89)); // Output: 1,234,567.89
Currency Formatting
This feature allows you to format numbers as currency. In this example, the number 1234567.89 is formatted as USD currency according to the 'en-US' locale.
const { NumberFormat } = require('@formatjs/intl-numberformat');
const formatter = new NumberFormat('en-US', { style: 'currency', currency: 'USD' });
console.log(formatter.format(1234567.89)); // Output: $1,234,567.89
Percentage Formatting
This feature allows you to format numbers as percentages. In this example, the number 0.1234 is formatted as a percentage according to the 'en-US' locale.
const { NumberFormat } = require('@formatjs/intl-numberformat');
const formatter = new NumberFormat('en-US', { style: 'percent' });
console.log(formatter.format(0.1234)); // Output: 12%
Custom Number Formatting
This feature allows you to customize the number of fraction digits. In this example, the number 1234567.89 is formatted to always show two decimal places according to the 'en-US' locale.
const { NumberFormat } = require('@formatjs/intl-numberformat');
const formatter = new NumberFormat('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
console.log(formatter.format(1234567.89)); // Output: 1,234,567.89
Other packages similar to @formatjs/intl-numberformat
numeral
Numeral.js is a library for formatting and manipulating numbers. It provides a wide range of formatting options, including currency, percentages, and custom formats. Unlike @formatjs/intl-numberformat, Numeral.js does not rely on the Intl API and offers its own set of formatting rules.
accounting
Accounting.js is a lightweight JavaScript library for number, money, and currency formatting. It is designed to be simple and easy to use, with a focus on financial calculations. While it offers similar functionalities to @formatjs/intl-numberformat, it is more specialized for accounting purposes.
numbro
Numbro.js is a JavaScript library for formatting and manipulating numbers. It supports a wide range of locales and provides features for currency, percentages, and custom number formats. Numbro.js is similar to @formatjs/intl-numberformat but offers additional features like parsing and unformatting numbers.