NumberFormatter

Number formatting for display, and its locale-independent counterpart.

format follows the OS locale, because a number shown to a user should read the way that user writes numbers. formatInvariant keeps a fixed dot separator and no grouping, for the few strings that are not prose: interop payloads another system parses, and values that get re-parsed rather than read.

Functions

Link copied to clipboard
fun format(value: Double, decimalPlaces: Int): String

Formats a double for display, using the locale's decimal and grouping separators.

fun format(value: Float, decimalPlaces: Int): String

Formats a float for display, using the locale's decimal and grouping separators.

Link copied to clipboard
fun formatInvariant(value: Double, decimalPlaces: Int): String

Formats with a fixed dot separator and no grouping, whatever the locale.

fun formatInvariant(value: Float, decimalPlaces: Int): String

Float overload of formatInvariant.

Link copied to clipboard

True when char is a decimal mark reachable from a Decimal/DecimalSigned keyboard.

Link copied to clipboard

True when text could still become a number as the user types — an empty field, a lone sign, or a trailing separator. A controlled field consults this after parseDecimalOrNull returns null, so transient input like - or -. survives on the way to -1.5 instead of snapping back to the last committed value.

Link copied to clipboard

Parses a user-entered decimal whose separator follows the keyboard locale — 48,21 on a comma locale, which String.toDoubleOrNull rejects. A lone separator is always the decimal mark; grouping is only recognised when more than one appears, in which case the last one is the decimal mark. Returns null for anything unparseable.