NumberFormat
t
type tcurrency
type currency = stringAn ISO 4217 currency code. e.g. USD, EUR, CNY
currencyDisplay
type currencyDisplay = [
| #code
| #name
| #narrowSymbol
| #symbol
]currencySign
type currencySign = [#accounting | #standard]notation
type notation = [
| #compact
| #engineering
| #scientific
| #standard
]compactDisplay
type compactDisplay = [#long | #short]Used only when notation is #compact
signDisplay
type signDisplay = [
| #always
| #auto
| #exceptZero
| #negative
| #never
]style
type style = [#currency | #decimal | #percent | #unit]unitSystem
type unitSystem = stringDefined in https://tc39.es/proposal-unified-intl-numberformat/section6/locales-currencies-tz_proposed_out.html#sec-issanctionedsimpleunitidentifier Only used when style is #unit
unitDisplay
type unitDisplay = [#long | #narrow | #short]Only used when style is #unit
rounding
type rounding = [
| #ceil
| #expand
| #floor
| #halfCeil
| #halfEven
| #halfExpand
| #halfFloor
| #halfTrunc
| #trunc
]roundingPriority
type roundingPriority = [
| #auto
| #lessPrecision
| #morePrecision
]roundingIncrement
type roundingIncrement = [
| #1
| #10
| #100
| #1000
| #2
| #20
| #200
| #2000
| #25
| #250
| #2500
| #5
| #50
| #500
| #5000
]trailingZeroDisplay
type trailingZeroDisplay = [
| #auto
| #lessPrecision
| #stripIfInteger
]options
type options = {
compactDisplay?: compactDisplay,
numberingSystem?: Intl_Common.numberingSystem,
currency?: currency,
currencyDisplay?: currencyDisplay,
currencySign?: currencySign,
localeMatcher?: Intl_Common.localeMatcher,
notation?: notation,
signDisplay?: signDisplay,
style?: style,
unit?: unitSystem,
unitDisplay?: unitDisplay,
useGrouping?: Grouping.t,
roundingMode?: rounding,
roundingPriority?: roundingPriority,
roundingIncrement?: roundingIncrement,
trailingZeroDisplay?: trailingZeroDisplay,
minimumIntegerDigits?: Intl_Common.oneTo21,
minimumFractionDigits?: Intl_Common.zeroTo20,
maximumFractionDigits?: Intl_Common.zeroTo20,
minimumSignificantDigits?: Intl_Common.oneTo21,
maximumSignificantDigits?: Intl_Common.oneTo21,
}resolvedOptions
type resolvedOptions = {
currency?: currency,
currencyDisplay?: currencyDisplay,
currencySign?: currencySign,
compactDisplay?: compactDisplay,
unit: unitSystem,
unitDisplay: unitDisplay,
roundingMode?: rounding,
roundingPriority?: roundingPriority,
roundingIncrement?: roundingIncrement,
minimumIntegerDigits?: Intl_Common.oneTo21,
minimumFractionDigits?: Intl_Common.zeroTo20,
maximumFractionDigits?: Intl_Common.zeroTo20,
minimumSignificantDigits?: Intl_Common.oneTo21,
maximumSignificantDigits?: Intl_Common.oneTo21,
locale: string,
notation: notation,
numberingSystem: Intl_Common.numberingSystem,
signDisplay: signDisplay,
style: style,
useGrouping: Grouping.t,
}supportedLocalesOptions
type supportedLocalesOptions = {
localeMatcher: Intl_Common.localeMatcher,
}numberFormatPartType
type numberFormatPartType = [
| #compact
| #currency
| #decimal
| #exponentInteger
| #exponentMinusSign
| #exponentSeparator
| #fraction
| #group
| #infinity
| #integer
| #literal
| #minusSign
| #nan
| #percentSign
| #plusSign
| #unit
| #unknown
]numberFormatPart
type numberFormatPart = {
\"type": numberFormatPartType,
value: string,
}rangeSource
type rangeSource = [#endRange | #shared | #startRange]numberFormatRangePart
type numberFormatRangePart = {
\"type": numberFormatPartType,
value: string,
source: rangeSource,
}make
let make: (~locales: array<string>=?, ~options: options=?) => tCreates a new Intl.NumberFormat instance for locale-aware number formatting.
See Intl.NumberFormat on MDN.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en-US"], ~options={style: #currency, currency: "USD"})
formatter->Intl.NumberFormat.format(1234.5) == "$1,234.50"
supportedLocalesOf
let supportedLocalesOf: (
array<string>,
~options: supportedLocalesOptions=?,
) => array<string>supportedLocalesOf(locales, ~options) filters locales to those supported for number formatting.
See Intl.NumberFormat.supportedLocalesOf on MDN.
Examples
RESCRIPTIntl.NumberFormat.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"]
resolvedOptions
let resolvedOptions: t => resolvedOptionsresolvedOptions(formatter) returns the actual options being used.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en-US"])
Intl.NumberFormat.resolvedOptions(formatter).locale == "en-US"
format
let format: (t, float) => stringformat(formatter, value) returns the formatted representation of value.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en-US"])
formatter->Intl.NumberFormat.format(1234.5) == "1,234.5"
formatRange
let formatRange: (t, ~start: float, ~end: float) => stringformatRange(formatter, ~start, ~end) formats numbers representing a range.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en-US"])
formatter->Intl.NumberFormat.formatRange(~start=1., ~end=2.)->String.length > 0
formatToParts
let formatToParts: (t, float) => array<numberFormatPart>formatToParts(formatter, value) breaks the formatted result into parts.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en-US"])
formatter->Intl.NumberFormat.formatToParts(123)->Array.length > 0
formatRangeToParts
let formatRangeToParts: (
t,
~start: float,
~end: float,
) => array<numberFormatRangePart>formatRangeToParts(formatter, ~start, ~end) returns how the range would be rendered.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en-US"])
formatter->Intl.NumberFormat.formatRangeToParts(~start=1., ~end=2.)->Array.length > 0
formatInt
let formatInt: (t, int) => stringformatInt(formatter, value) formats integer values.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en"])
formatter->Intl.NumberFormat.formatInt(42) == "42"
formatIntRange
let formatIntRange: (t, ~start: int, ~end: int) => stringformatIntRange(formatter, ~start, ~end) formats integer ranges.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en"])
formatter->Intl.NumberFormat.formatIntRange(~start=1, ~end=3)->String.length > 0
formatIntToParts
let formatIntToParts: (t, int) => array<numberFormatPart>formatIntToParts(formatter, value) returns formatting parts for an integer.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en"])
formatter->Intl.NumberFormat.formatIntToParts(123)->Array.length > 0
formatIntRangeToParts
let formatIntRangeToParts: (t, ~start: int, ~end: int) => array<numberFormatRangePart>formatIntRangeToParts(formatter, ~start, ~end) returns how the integer range would be rendered.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en"])
formatter->Intl.NumberFormat.formatIntRangeToParts(~start=1, ~end=1)->Array.length > 0
formatBigInt
let formatBigInt: (t, bigint) => stringformatBigInt(formatter, value) formats bigint values.
formatBigIntRange
let formatBigIntRange: (t, ~start: bigint, ~end: bigint) => stringformatBigIntRange(formatter, ~start, ~end) formats a range of bigint values.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en"])
formatter->Intl.NumberFormat.formatBigIntRange(~start=1n, ~end=2n) == "1–2"
formatBigIntToParts
let formatBigIntToParts: (t, bigint) => array<numberFormatPart>formatBigIntToParts(formatter, value) returns the bigint formatting broken into parts.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en"])
formatter->Intl.NumberFormat.formatBigIntToParts(5n)->Array.length > 0
formatBigIntRangeToParts
let formatBigIntRangeToParts: (
t,
~start: bigint,
~end: bigint,
) => array<numberFormatRangePart>formatBigIntRangeToParts(formatter, ~start, ~end) describes how the bigint range would be rendered.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en"])
formatter->Intl.NumberFormat.formatBigIntRangeToParts(~start=3n, ~end=4n)->Array.length > 0
formatString
let formatString: (t, string) => stringformatString(formatter, value) interprets value as a number string and formats it.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en"])
formatter->Intl.NumberFormat.formatString("1234") == "1,234"
formatStringToParts
let formatStringToParts: (t, string) => array<numberFormatPart>formatStringToParts(formatter, value) returns formatting parts for a numeric string.
Examples
RESCRIPTlet formatter = Intl.NumberFormat.make(~locales=["en"])
formatter->Intl.NumberFormat.formatStringToParts("123")->Array.length > 0
ignore
let ignore: t => unitignore(numberFormat) ignores the provided numberFormat and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.