--- parts: - Format Numbers - Localize - Systems title: 'Localize: Format Numbers' eleventyNavigation: key: Systems >> Localize >> Format Numbers title: Format Numbers order: 30 parent: Systems >> Localize --- # Systems >> Localize >> Format Numbers ||30 ## Features - Small file size - Uses `Intl.NumberFormat` but patches browser inconsistencies ## Formatting With the `formatNumber` you can safely display a number for all languages. The input value is `1234.56`. ```js script import { html, css } from '@mdjs/mdjs-preview'; import { formatNumber, formatNumberToParts } from '@lion/ui/localize.js'; import allLocales from './assets/all-locales.js'; const formatNumberDemoStyle = css` .demo-table { border-collapse: collapse; text-align: right; } .demo-table thead > tr { border-bottom: 1px solid grey; } .demo-table thead > tr > :first-child, .demo-table tbody > tr > :first-child, .demo-table tfoot > tr > :first-child { text-align: left; } .demo-table th, .demo-table td { padding: 8px; } `; const value = 1234.56; ``` ```js preview-story export const formatting = () => html`
Options Output Code
Default ${formatNumber(value)}
Currency symbol ${formatNumber(value, { style: 'currency', currencyDisplay: 'symbol', currency: 'EUR', })} formatNumber({ style: 'currency', currencyDisplay: 'symbol', currency: 'EUR' })
Currency code ${formatNumber(value, { style: 'currency', currencyDisplay: 'code', currency: 'EUR', })} formatNumber({ style: 'currency', currencyDisplay: 'code', currency: 'EUR' })
Locale ${formatNumber(value, { locale: 'nl-NL' })} formatNumber({ locale: 'nl-NL' })
No decimals ${formatNumber(value, { minimumFractionDigits: 0, maximumFractionDigits: 0, })} formatNumber({ minimumFractionDigits: 0, maximumFractionDigits: 0, })
`; ``` ## Formatting parts `formatNumberToParts` allows to get individual parts of a number on all browsers. The input value `1234.56` gets formatted via ```js formatNumberToParts(value, { style: 'currency', currency: 'EUR' }); ``` ```js preview-story export const formattingParts = () => html` ${formatNumberToParts(1234.56, { style: 'currency', currency: 'EUR' }).map( part => html` `, )}
Part Output
${part.type} ${part.value}
`; ``` ## List common locales The input value is `1234.56`. Formatting happens via ```js formatNumber(1234.56, { locale, style: 'currency', currency: 'EUR' }); formatNumber(1234.56, { locale, style: 'currency', currency: 'USD' }); ``` ```js preview-story export const listCommonLocales = () => html` ${['en-GB', 'en-US', 'nl-NL', 'nl-BE', 'fr-FR', 'de-DE'].map( locale => html` `, )}
Locale Output Euro Output US Dollar
${locale} ${formatNumber(1234.56, { locale, style: 'currency', currency: 'EUR' })} ${formatNumber(1234.56, { locale, style: 'currency', currency: 'USD' })}
`; ``` ## List all locales The following list show number formatting for all known locales. The input value is `1234.56`. Formatting happens via ```js formatNumber(1234.56, { locale, style: 'currency', currency: 'EUR' }); formatNumber(1234.56, { locale, style: 'currency', currency: 'USD' }); ``` ```js preview-story export const listAllLocales = () => html` ${Object.keys(allLocales).map( locale => html` `, )}
Locale Output Euro Output US Dollar
${locale} ${formatNumber(1234.56, { locale, style: 'currency', currency: 'EUR' })} ${formatNumber(1234.56, { locale, style: 'currency', currency: 'USD' })}
`; ```