What is @messageformat/number-skeleton?
@messageformat/number-skeleton is a utility for working with ICU Number Skeletons, which are a way to format numbers in a locale-aware manner. This package allows you to create, parse, and manipulate number skeletons to format numbers according to various internationalization (i18n) rules.
What are @messageformat/number-skeleton's main functionalities?
Creating Number Skeletons
This feature allows you to create a number skeleton for a specific format, such as currency. The code sample demonstrates creating a number skeleton for USD currency and formatting a number accordingly.
const { NumberSkeleton } = require('@messageformat/number-skeleton');
const skeleton = new NumberSkeleton('currency/USD');
console.log(skeleton.format(1234.56)); // $1,234.56
Parsing Number Skeletons
This feature allows you to parse a number skeleton string into a usable format. The code sample shows how to parse a number skeleton string and use it to format a number.
const { parseNumberSkeleton } = require('@messageformat/number-skeleton');
const skeleton = parseNumberSkeleton('currency/USD unit-width-narrow');
console.log(skeleton.format(1234.56)); // $1,234.56
Custom Number Formatting
This feature allows you to create custom number formats using number skeletons. The code sample demonstrates creating a number skeleton for percentage formatting with integer precision.
const { NumberSkeleton } = require('@messageformat/number-skeleton');
const skeleton = new NumberSkeleton('percent precision-integer');
console.log(skeleton.format(0.1234)); // 12%
Other packages similar to @messageformat/number-skeleton
intl
The 'intl' package provides a comprehensive set of internationalization features, including number formatting. It is a more general-purpose library compared to @messageformat/number-skeleton, which is specialized for number skeletons.
numeral
The 'numeral' package is a library for formatting and manipulating numbers. It offers a wide range of formatting options but does not specifically focus on ICU Number Skeletons like @messageformat/number-skeleton.
numbro
The 'numbro' package is another library for formatting and manipulating numbers. It supports various locales and formats but does not provide the same level of integration with ICU Number Skeletons as @messageformat/number-skeleton.
@messageformat/number-skeleton
Tools for working with ICU NumberFormat skeletons and patterns.
import {
getNumberFormatter,
getNumberFormatterSource,
NumberFormatError,
parseNumberPattern,
parseNumberSkeleton,
Skeleton,
Unit
} from '@messageformat/number-skeleton';
The package is released as an ES module only. If using from a CommonJS context, you may need to import()
it, or use a module loader like esm.
Uses Intl.NumberFormat internally, including features provided by the Unified API Proposal.
Classes
Class | Description |
---|
NumberFormatError | Base class for errors. In addition to a code and a human-friendly message , may also includes the token stem as well as other fields. |
Functions
Interfaces
Interface | Description |
---|
Skeleton | An object representation of a parsed string skeleton, with token values grouped by type. |
Type Aliases
Messageformat is an OpenJS Foundation project, and we follow its Code of Conduct.