What is @formatjs/intl-unified-numberformat?
@formatjs/intl-unified-numberformat is a polyfill for the ECMAScript Internationalization API's Unified NumberFormat, which provides a way to format numbers in a locale-sensitive manner. This package is particularly useful for applications that need to support multiple locales and require consistent number formatting across different environments.
What are @formatjs/intl-unified-numberformat's main functionalities?
Basic Number Formatting
This feature allows you to format numbers according to the locale specified. In this example, the number 1234567.89 is formatted according to the 'en-US' locale.
const UnifiedNumberFormat = require('@formatjs/intl-unified-numberformat');
const formatter = new UnifiedNumberFormat('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 UnifiedNumberFormat = require('@formatjs/intl-unified-numberformat');
const formatter = new UnifiedNumberFormat('en-US', { style: 'currency', currency: 'USD' });
console.log(formatter.format(1234567.89)); // Output: $1,234,567.89
Percent 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 UnifiedNumberFormat = require('@formatjs/intl-unified-numberformat');
const formatter = new UnifiedNumberFormat('en-US', { style: 'percent' });
console.log(formatter.format(0.1234)); // Output: 12%
Unit Formatting
This feature allows you to format numbers with units. In this example, the number 60 is formatted as miles per hour according to the 'en-US' locale.
const UnifiedNumberFormat = require('@formatjs/intl-unified-numberformat');
const formatter = new UnifiedNumberFormat('en-US', { style: 'unit', unit: 'mile-per-hour' });
console.log(formatter.format(60)); // Output: 60 mph
Other packages similar to @formatjs/intl-unified-numberformat
intl
The 'intl' package is a polyfill for the ECMAScript Internationalization API, which includes number formatting, date and time formatting, and more. It is a more comprehensive package compared to @formatjs/intl-unified-numberformat, which focuses specifically on number formatting.
numeral
The 'numeral' package 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-unified-numberformat, it does not focus on locale-specific formatting.
accounting
The 'accounting' package is a lightweight library for number, money, and currency formatting. It is designed for financial applications and provides simple and straightforward formatting options. It does not offer the same level of locale-specific formatting as @formatjs/intl-unified-numberformat.
intl-unified-numberformat
A ponyfill/polyfill for intl-unified-numberformat
. This wraps Intl.NumberFormat
and has the exact same APIs.
Installation
npm install @formatjs/intl-unified-numberformat
Requirements
This package requires the following capabilities:
Features
Everything in the https://github.com/tc39/proposal-unified-intl-numberformat proposal with the caveats below.
Caveats
compact
notation is currently buggy in certain locales with special compact rules (such as zh-Hant
or Somali
) See https://github.com/tc39/proposal-unified-intl-numberformat/issues/26 for more details.
Usage
To use the ponyfill, import it along with its data:
import {UnifiedNumberFormat} from '@formatjs/intl-unified-numberformat';
UnifiedNumberFormat.__addLocaleData(
require('@formatjs/intl-unified-numberformat/dist/locale-data/zh.json')
);
UnifiedNumberFormat.__addLocaleData(
require('@formatjs/intl-unified-numberformat/dist/locale-data/en.json')
);
new UnifiedNumberFormat('zh', {
style: 'unit',
unit: 'bit',
unitDisplay: 'long',
}).format(1000);
To use this as a polyfill, override Intl.NumberFormat
as below:
import '@formatjs/intl-unified-numberformat/polyfill';
if (typeof Intl.NumberFormat.__addLocaleData === 'function') {
Intl.NumberFormat.__addLocaleData(
require('@formatjs/intl-unified-numberformat/dist/locale-data/zh.json')
);
Intl.NumberFormat.__addLocaleData(
require('@formatjs/intl-unified-numberformat/dist/locale-data/en.json')
);
}
new Intl.NumberFormat('zh', {
style: 'unit',
unit: 'bit',
unitDisplay: 'long',
}).format(1000);
new Intl.NumberFormat('en-US', {
notation: 'engineering',
}).format(987654321);
new Intl.NumberFormat('zh', {
style: 'currency',
currency: 'EUR',
currencySign: 'accounting',
}).format(-100);
Supported Units
Currently intl-unified-numberformat has a list of sanctioned units as below
type Unit =
| 'acre'
| 'bit'
| 'byte'
| 'celsius'
| 'centimeter'
| 'day'
| 'degree'
| 'fahrenheit'
| 'fluid-ounce'
| 'foot'
| 'gallon'
| 'gigabit'
| 'gigabyte'
| 'gram'
| 'hectare'
| 'hour'
| 'inch'
| 'kilobit'
| 'kilobyte'
| 'kilogram'
| 'kilometer'
| 'liter'
| 'megabit'
| 'megabyte'
| 'meter'
| 'mile'
| 'mile-scandinavian'
| 'millimeter'
| 'milliliter'
| 'millisecond'
| 'minute'
| 'month'
| 'ounce'
| 'percent'
| 'petabyte'
| 'pound'
| 'second'
| 'stone'
| 'terabit'
| 'terabyte'
| 'week'
| 'yard'
| 'year';