Format
Numbers, units and lists in the reader's region
Formats numbers, currencies, percentages, byte counts, lists and measurements using the device's locale: grouping separators, decimal marks, unit systems and list conjunctions. A hand-rolled k/m/b shortener or a hardcoded "km" is wrong outside the region it was written in.
Properties
usesMetricBooleanTrue where this device's region uses the metric system.
localeStringThe device's locale identifier, e.g. "en_US".
Methods
Format.number(value, options?)StringDecimal number.
- value Number required
The number to format.
- options Any
- digits Number
Maximum decimal places. Default 0.
- minDigits Number
Minimum decimal places, padded with zeros. Default 0.
- grouping Boolean
Thousands separator. Default true.
- digits Number
Format.compact(value, options?)StringShortened number, 12500 becomes "13K", localised.
- value Number required
The number to shorten.
- options Any
- digits Number
Maximum decimal places. Default 1.
- digits Number
Format.percent(value, options?)StringPercentage from a fraction: 0.42 becomes "42%".
- value Number required
A fraction, not 0 to 100. 0.42 gives "42%".
- options Any
- digits Number
Maximum decimal places. Default 0.
- digits Number
Format.currency(value, options?)StringMoney. Defaults to the region's currency.
- value Number required
The amount.
- options Any
- code String
ISO 4217 code such as "EUR". Defaults to the device's currency.
- digits Number
Fixed decimal places. Defaults to what the currency uses.
- code String
Format.bytes(value, options?)StringByte count, 1048576 becomes "1 MB". ("file" or "memory").
- value Number required
A count of bytes.
- options Any
- style String
"memory" counts in powers of 1024. Anything else counts in powers of 1000, which is what a file size uses.
- style String
Format.ordinal(value)StringOrdinal: 3 becomes "3rd".
- value Number required
Format.list(items)StringJoins with the region's conjunction: "a, b, and c".
- items [String] required
The strings to join.
returns The list joined the way the device's language does it, conjunction included.
Format.measurement(value, options)StringA value with a unit, converted to whatever the region uses. unit is required. One of millimeters, centimeters, meters, kilometers, inches, feet, yards, miles, celsius, fahrenheit, kelvin, metersPerSecond, kilometersPerHour, milesPerHour, knots, grams, kilograms, ounces, pounds, kilocalories, kilojoules. Set to to pin the output unit instead of letting the locale choose.
- value Number required
The amount, in the unit named below.
- options Any required
- unit String required
The unit
valueis in. - to String
Convert to this unit. Left out, the device's region decides.
- style String
"short", "medium" or "long", for "km", "km" or "kilometers".
- digits Number
Maximum decimal places.
- unit String required
Examples
A stats row
Column({ children: [
Text(Format.compact(12500)), // "13K"
Text(Format.percent(0.42)), // "42%"
Text(Format.measurement(4213, { unit: "meters" })), // "4.2 km" or "2.6 mi"
Text(Format.list(["Mon", "Wed", "Fri"])), // "Mon, Wed, and Fri"
]})