Intl.NumberFormat
A library for formatting numbers in a language sensitve way. This module binds to Intl.NumberFormat.
Create
type NumberFormat
A NumberFormat object, for formatting numbers in a language sensitive way.
fromLocale
:
Intl.Locale.Locale -> Intl.NumberFormat.NumberFormat
Create a NumberFormat using rules from the specified language
format (fromLocale "ar") 42
-- "٤٢"
fromOptions
:
Intl.NumberFormat.Options -> Intl.NumberFormat.NumberFormat
Create a NumberFormat using rules from the language and other options.
let
formatPercent = fromOptions
{ defaults |
, style = PercentStyle
, maximumSignificantDigits = Just 3
}
|> format
in
formatPercent 0.12345 -- 12.3%
Formatting Dates
format
:
Intl.NumberFormat.NumberFormat -> number -> String
Format a number according to the rules of the NumberFormat.
format (fromLocale Locale.en) 456789.123
Support and options
Not all environments will support all languages and options. These functions help determine what is supported, and what options a particular NumberFormat will use.
type alias Options =
{ locale : Intl.Locale.Locale
, style : Intl.NumberFormat.Style
, currency : Intl.Currency.Currency
, currencyDisplay : Intl.NumberFormat.CurrencyDisplay
, useGrouping : Bool
, minimumIntegerDigits : Maybe.Maybe Int
, minimumFractionDigits : Maybe.Maybe Int
, maximumFractionDigits : Maybe.Maybe Int
, minimumSignificantDigits : Maybe.Maybe Int
, maximumSignificantDigits : Maybe.Maybe Int
}
An Options record, containing the possible settings for a NumberFormat object.
The min/max properties fall into two groups: minimumIntegerDigits
,
minimumFractionDigits
, and maximumFractionDigits
in one group,
minimumSignificantDigits
and maximumSignificantDigits
in the other. If at
least one property from the second group is not Nothing
, then the first group
is ignored.
type Style
= PercentStyle
| CurrencyStyle
| DecimalStyle
Style of the number format.
type CurrencyDisplay
= CurrencyCode
| CurrencyName
| CurrencySymbol
How to display the currency information.
defaults
:
Intl.NumberFormat.Options
Returns the default options. This is helpful if you only care to change a few options.
options : Options
options =
{ defaults | style = PercentStyle }
resolvedOptions
:
Intl.NumberFormat.NumberFormat -> Intl.NumberFormat.Options
Returns the locale and formatting options computed when the NumberFormat was created.
case (resolvedOptions numberFormat).style of
CurrencyStyle -> "Total Money: "
PercentStyle -> "Total Percent: "
DecimalStyle -> "Total Quantity: "
supportedLocalesOf
:
List Intl.Locale.Locale -> List Intl.Locale.Locale
Returns a list from the provided languages that are supported without having to fall back to the runtime's default language.
case fromLanguageTag "tlh" of
Just klingon ->
if isEmpty (supportedLocalesOf [ klingon ]) then
"I can't sort Klingon text"
else
"Make it so, Number One"
Nothing ->
"Khaaaaan!"