Socket
Socket
Sign inDemoInstall

@formatjs/intl-unified-numberformat

Package Overview
Dependencies
1
Maintainers
3
Versions
43
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @formatjs/intl-unified-numberformat

Ponyfill for intl unified numberformat proposal


Version published
Weekly downloads
229K
increased by6.62%
Maintainers
3
Install size
48.4 MB
Created
Weekly downloads
 

Package description

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

Readme

Source

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

  1. 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') // 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.__addLocaleData === 'function') {
  Intl.NumberFormat.__addLocaleData(
    require('@formatjs/intl-unified-numberformat/dist/locale-data/zh.json') // locale-data for zh
  );
  Intl.NumberFormat.__addLocaleData(
    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比特

new Intl.NumberFormat('en-US', {
  notation: 'engineering',
}).format(987654321); // 987.7E6

new Intl.NumberFormat('zh', {
  style: 'currency',
  currency: 'EUR',
  currencySign: 'accounting',
}).format(-100); // (€100.00)

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';

Keywords

FAQs

Last updated on 27 Jan 2020

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc