Table of contents
Installation
@sumup/intl
needs to be installed as a dependency via the Yarn or npm package managers. The npm CLI ships with Node. You can read how to install the Yarn CLI in their documentation. @sumup/intl
requires Node v10.18+.
Depending on your preference, run one of the following:
$ yarn add @sumup/intl
$ npm install @sumup/intl
@sumup/intl
wraps the ECMAScript Internationalization API which is supported by all modern browsers (note that formatToParts is not supported by IE11). If you need to support older browsers, you need to include a polyfill for the Intl.NumberFormat
API.
Node supports the Intl
API since v8, however, it includes only the English localisations up to v12. Node v13 and above support all locales. If you're unable to use Node v13+, you can either include a polyfill for the Intl.NumberFormat
API or use a custom Node build.
Usage
All functions exported by @sumup/intl
share a similar interface such as the common locales
, options
, and currency
arguments. These are passed on almost unchanged to the Intl.NumberFormat
constructor and thus support the same values. If the locales
argument is not provided or is undefined, the runtime's default locale is used. Please refer to the MDN documentation for more details.
Format as string
format
Formats a number according to the locale with support for various styles, units, and notations.
Arguments
Name | Type | Examples |
---|
value | number | 12345.67 , -0.89 |
locales? | string | string[] | 'de-DE' , 'DE' , 'zh-Hans-CN' , ['de-AT', 'de-DE'] |
options? | Intl.NumberFormatOptions | { style: 'unit', unit: 'mile-per-hour' } |
Examples
import { format } from '@sumup/intl';
format(12345.67, 'de-DE');
format(-0.89, ['ban', 'id']);
format(16, 'en-GB', { style: 'unit', unit: 'liter', unitDisplay: 'long' });
formatCurrency
Formats a number according to the locale in the country's official curreny with support for various notations.
Arguments
Name | Type | Examples |
---|
value | number | 12345.67 , -0.89 |
locales? | string | string[] | 'de-DE' , 'DE' , 'zh-Hans-CN' , ['de-AT', 'de-DE'] |
currency? | string | 'EUR' , 'BRL' , 'USD' |
options? | Intl.NumberFormatOptions | { style: 'unit', unit: 'mile-per-hour' } |
Examples
import { formatCurrency } from '@sumup/intl';
formatCurrency(12345.67, 'de-DE');
formatCurrency(89, 'ja-JP', 'JPY');
formatCurrency(16, 'en-GB', null, { currencyDisplay: 'name' });
Format as parts
formatToParts
Formats a number according to the locale with support for various styles, units, and notations.
Arguments
Name | Type | Examples |
---|
value | number | 12345.67 , -0.89 |
locales? | string | string[] | 'de-DE' , 'DE' , 'zh-Hans-CN' , ['de-AT', 'de-DE'] |
options? | Intl.NumberFormatOptions | { style: 'unit', unit: 'mile-per-hour' } |
import { formatToParts } from '@sumup/intl';
formatToParts(12345.67, 'de-DE');
formatToParts(-0.89, ['ban', 'id']);
formatToParts(16, 'en-GB', {
style: 'unit',
unit: 'liter',
unitDisplay: 'long',
});
formatCurrencyToParts
Formats a number according to the locale in the country's official curreny with support for various notations.
Arguments
Name | Type | Examples |
---|
value | number | 12345.67 , -0.89 |
locales? | string | string[] | 'de-DE' , 'DE' , 'zh-Hans-CN' , ['de-AT', 'de-DE'] |
currency? | string | 'EUR' , 'BRL' , 'USD' |
options? | Intl.NumberFormatOptions | { style: 'unit', unit: 'mile-per-hour' } |
Examples
import { formatCurrencyToParts } from '@sumup/intl';
formatCurrencyToParts(12345.67, 'de-DE');
formatCurrencyToParts(-89, 'ja-JP', 'JPY');
formatCurrencyToParts(16, 'en-GB', null, { currencyDisplay: 'name' });
Resolve format
resolveFormat
Resolves the locale and collation options that are used to format a number.
Arguments
Name | Type | Examples |
---|
locales? | string | string[] | 'de-DE' , 'DE' , 'zh-Hans-CN' , ['de-AT', 'de-DE'] |
options? | Intl.NumberFormatOptions | { style: 'unit', unit: 'mile-per-hour' } |
Examples
import { resolveFormat } from '@sumup/intl';
resolveFormat();
resolveFormat(['ban', 'id']);
resolveFormat('en-GB', { style: 'unit', unit: 'liter', unitDisplay: 'long' });
resolveCurrencyFormat
Resolves the locale and collation options that are used to format a number in the country's official currency.
Arguments
Name | Type | Examples |
---|
locales? | string | string[] | 'de-DE' , 'DE' , 'zh-Hans-CN' , ['de-AT', 'de-DE'] |
currency? | string | 'EUR' , 'BRL' , 'USD' |
options? | Intl.NumberFormatOptions | { style: 'unit', unit: 'mile-per-hour' } |
Examples
import { resolveCurrencyFormat } from '@sumup/intl';
resolveCurrencyFormat();
resolveCurrencyFormat('ja-JP');
resolveCurrencyFormat('en-GB', { currencyDisplay: 'name' });
Constants
isNumberFormatSupported
Whether the Intl
and Intl.NumberFormat
APIs are supported by the runtime.
const isNumberFormatSupported: boolean;
isNumberFormatToPartsSupported
Whether the Intl
, Intl.NumberFormat
, and Intl.NumberFormat.formatToParts
APIs are supported by the runtime.
const isNumberFormatToPartsSupported: boolean;
CURRENCIES
An object that maps a 2 char country code to its official 3 char currency code. View all supported countries.
const CURRENCIES: { [country: string]: string };
Code of Conduct
We want to foster an inclusive and friendly community around our Open Source efforts. Like all SumUp Open Source projects, this project follows the Contributor Covenant Code of Conduct. Please, read it and follow it.
If you feel another member of the community violated our CoC or you are experiencing problems participating in our community because of another individual's behavior, please get in touch with our maintainers. We will enforce the CoC.
Maintainers
About SumUp
SumUp is a mobile-point of sale provider. It is our mission to make easy and fast card payments a reality across the entire world. You can pay with SumUp in more than 30 countries, already. Our engineers work in Berlin, Cologne, Sofia, and Sāo Paulo. They write code in JavaScript, Swift, Ruby, Go, Java, Erlang, Elixir, and more.
Want to come work with us? Head to our careers page to find out more.