Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@formatjs/intl-unified-numberformat
Advanced tools
@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.
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
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.
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.
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.
npm install @formatjs/intl-unified-numberformat
This package requires the following capabilities:
unit
, unitDisplay
, style: unit
and sanctioned units are supportedformatToParts
does not include the unit
part yet.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') // locale-data for zh
);
UnifiedNumberFormat.__addLocaleData(
require('@formatjs/intl-unified-numberformat/dist/locale-data/en.json') // locale-data for en
);
new UnifiedNumberFormat('zh', {
style: 'unit',
unit: 'bit',
unitDisplay: 'long',
}).format(1000); // 1,000比特
To use this as a polyfill, override Intl.NumberFormat
as below:
import '@formatjs/intl-unified-numberformat/polyfill';
if (typeof Intl.NumberFormat.__addUnitLocaleData === 'function') {
Intl.NumberFormat.__addUnitLocaleData(
require('@formatjs/intl-unified-numberformat/dist/locale-data/zh.json') // locale-data for zh
);
Intl.NumberFormat.__addUnitLocaleData(
require('@formatjs/intl-unified-numberformat/dist/locale-data/en.json') // locale-data for en
);
}
new Intl.NumberFormat('zh', {
style: 'unit',
unit: 'bit',
unitDisplay: 'long',
}).format(1000); // 1,000比特
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';
FAQs
Ponyfill for intl unified numberformat proposal
The npm package @formatjs/intl-unified-numberformat receives a total of 209,179 weekly downloads. As such, @formatjs/intl-unified-numberformat popularity was classified as popular.
We found that @formatjs/intl-unified-numberformat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.