Intl.NumberFormat (ESNext)
Esta página fue traducida por PageTurner AI (beta). No está respaldada oficialmente por el proyecto. ¿Encontraste un error? Reportar problema →
Un polyfill para ESNext Intl.NumberFormat y Number.prototype.toLocaleString.
Instalación
- npm
- yarn
npm i @formatjs/intl-numberformat
yarn add @formatjs/intl-numberformat
Requisitos
Este paquete requiere las siguientes capacidades:
Características
Todo lo que incluye la especificación de la API de Internacionalización ES2020 (https://tc39.es/ecma402).
Uso
Mediante polyfill-fastly.io
Puedes usar el Constructor de URLs de polyfill-fastly.io para crear una etiqueta de script de polyfill para Intl.NumberFormat. Por defecto, la URL generada no incluye datos de localización. Para añadirlos, agrega Intl.NumberFormat.~locale.<locale> junto con los datos de localización de cualquier polyfill requerido a tu lista de características. Por ejemplo:
<!-- 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>
O si Intl.PluralRules también necesita polyfill:
<!-- 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>
Sencillo
import '@formatjs/intl-numberformat/polyfill'
import '@formatjs/intl-numberformat/locale-data/en' // locale-data for en
Importación dinámica + detección de capacidades
import {shouldPolyfill} from '@formatjs/intl-numberformat/should-polyfill'
async function polyfill(locale: string) {
const unsupportedLocale = shouldPolyfill(locale)
// This locale is supported
if (!unsupportedLocale) {
return
}
// Load the polyfill 1st BEFORE loading data
await import('@formatjs/intl-numberformat/polyfill-force')
await import(`@formatjs/intl-numberformat/locale-data/${unsupportedLocale}`)
}
Unidades admitidas
Unidades simples
Actualmente, la especificación define la siguiente lista de unidades permitidas:
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'
Unidades compuestas
Puedes especificar unidades X-per-Y, donde X e Y son unidades simples permitidas (ej. kilometer-per-hour).
La biblioteca elegirá el patrón localizado que mejor se ajuste para formatear esta unidad compuesta.