Number.prototype.toLocaleString
npm i @formatjs/intl-numberformat
yarn add @formatjs/intl-numberformat
This package requires the following capabilities:
Intl.getCanonicalLocales
Intl.Locale
Intl.PluralRules
Everything in the ES2020 Internationalization API spec (https://tc39.es/ecma402).
You can use polyfill-fastly.io URL Builder to create a polyfill script tag for Intl.NumberFormat. By default the created URL does not come with any locale data. In order to add locale data, append Intl.NumberFormat.~locale.<locale>, as well as locale data for any required polyfills, to your list of features. For example:
Intl.NumberFormat
Intl.NumberFormat.~locale.<locale>
<!-- Polyfill Intl.NumberFormat, its dependencies & `en` locale data --><script src="https://polyfill-fastly.io/v3/polyfill.min.js?features=Intl.NumberFormat,Intl.NumberFormat.~locale.en"></script>
Or if Intl.PluralRules needs to be polyfilled as well:
<!-- Polyfill Intl.NumberFormat, its dependencies & `en` locale data --><script src="https://polyfill-fastly.io/v3/polyfill.min.js?features=Intl.NumberFormat,Intl.NumberFormat.~locale.en,Intl.PluralRules.~locale.en"></script>
import '@formatjs/intl-numberformat/locale-data/en.js' / locale-data for en
import {shouldPolyfill} from '@formatjs/intl-numberformat/locale-data/${unsupportedLocale}.js` )}
Currently, the spec defines 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'
You can specify X-per-Y unit, where X and Y are sanctioned simple units (e.g. kilometer-per-hour). The library will choose the best-fit localized pattern to format this compound unit.
X-per-Y
X
Y
kilometer-per-hour