MetricUnitFormatUtils
Writes measurements for people, in the style of JavaScript's Intl.NumberFormat with style: "unit": a
localized number, then the unit's symbol or name. The unit is picked to suit the value's size unless one is
given, so 1,500 N is 1.5 kN. Takes a MetricUnit or its plain MetricUnitData.
Numbers go through NumberLocalizationUtils, so digit grouping and the decimal mark follow the locale. Unit names come from MetricUnitFallbackStrings, and duration words from TimeLocalizationUtils.
A duration is broken into years, months, days, hours, minutes and seconds with MetricUnit's own averaged year
and month (a Julian year, and a twelfth of it), not @quenty/time's 365 and 30 days, so MetricUnit.years(1)
reads 1 year and MetricUnit.months(1) reads 1 month. Only @quenty/time's words are used. For a clock-style
h:mm:ss, or calendar months, hand exact seconds to TimeDurationUtils or Time yourself.
MetricUnitFormatUtils.format(MetricUnit.meters(1500)) --> 1.5 km
MetricUnitFormatUtils.format(MetricUnit.meters(1500), { unitDisplay = "long" }) --> 1.5 kilometers
MetricUnitFormatUtils.format(MetricUnit.meters(1500):ToUSCustomary()) --> 4,921.26 ft
MetricUnitFormatUtils.format({ amount = 1234.5, dimensions = { length = 1 } }, { unit = "meter", locale = "de-de" }) --> 1 234,5 m
MetricUnitFormatUtils.format(MetricUnit.minutes(90)) --> 1 hour, 30 minutes
MetricUnitFormatUtils.formatUnit("kilowattHour", 3) --> 3 kWh
Types
UnitDisplay
type UnitDisplay = "short" | "long""short" writes the unit's symbol (5 km), "long" its name (5 kilometers).
MetricUnitData
interface MetricUnitData {amount: numberdimensions: {[string]: number}absolute: boolean?}A measurement as plain data, for saves and for formatting without a [MetricUnit]: its amount in SI base units, its exponents, and whether it is absolute. MetricUnit.toData and MetricUnit.fromData convert to and from it.
DurationStyle
type DurationStyle = "words" | "clock"How a duration is written: in words through @quenty/time's duration phrases, or as a clock.
MetricUnitTranslatorLike
interface MetricUnitTranslatorLike {FormatByKey: (self,key: string,args: {[string]: any}?) → stringGetLocaleId: (self) → string}
What the formatter needs of [MetricUnitTranslator]: a JSONTranslator's FormatByKey, GetLocaleId and,
for RxMetricUnitFormatUtils.observeFormat, ObserveFormatByKey.
MetricUnitFormatOptions
interface MetricUnitFormatOptions {locale: string?--
Picks the number format and unit names, defaults to en-us
unit: UnitLike?--
Always write this unit; for a rate, the whole rate's (kilometerPerHour, perHour) or the numerator's (kilometer)
autoUnit: boolean?--
Pick the unit that suits the size (the default), or always write the SI base unit
minimumFractionDigits: number?--
Pad with zeros to at least this many decimals, defaults to 0
maximumFractionDigits: number?--
Round to at most this many decimals, defaults to 3
maximumSignificantDigits: number?--
Round to this many significant digits instead of fraction digits
useGrouping: boolean?--
Group thousands for the locale, defaults to true
strings: UnitStringOverrides?--
Overrides a unit's phrase, e.g. { kilometer = { short = "%s klicks" } }
maxParts: number?--
How many non-zero units a duration is written in, largest first, defaults to 2
separator: string?--
Between the parts of a duration, defaults to ,
durationStyle: DurationStyle?--
words (the default, 1 hour, 30 minutes) or clock (1:30:00, or 1:30 under an hour)
durationTemplate: string?--
A TimeDurationUtils.format template such as hh:mm:ss; years and months are refused
translator: MetricUnitTranslatorLike?--
MetricUnitTranslator, for long names in the player's language
}Options for MetricUnitFormatUtils.format, modelled on Intl.NumberFormat.
Properties
NO_BREAK_SPACE
MetricUnitFormatUtils.NO_BREAK_SPACE: stringThe no-break space (U+00A0) that joins a number to its unit, so a line never wraps between them.
Functions
format
Writes a measurement for the locale in a unit that suits its size, or the one options.unit names. Durations are
broken into parts with localized words, temperatures keep whether they are absolute (5 °C) or a difference
(5 K), and a rate is written per the shortest time unit it fills at least once (1.6 MB/s).
A value is written in the system of the unit it was made from, so MetricUnit.new(5, "mile") writes miles and
MetricUnit.kilometers(8) kilometres. Convert it to write another (MetricUnit.ToMetric,
MetricUnit.ToImperial), or use MetricUnitSystem.Format. The locale never chooses a measurement system.
local length = MetricUnit.meters(2500)
MetricUnitFormatUtils.format(length) --> 2.5 km
MetricUnitFormatUtils.format(length, { unit = "meter" }) --> 2,500 m
MetricUnitFormatUtils.format(length, { unit = "stud" }) --> 8,928.571 studs
MetricUnitFormatUtils.format(length, { unitDisplay = "long", locale = "en-gb" }) --> 2.5 kilometres
MetricUnitFormatUtils.format(length, { maximumSignificantDigits = 1 }) --> 3 km
formatUnit
MetricUnitFormatUtils.formatUnit() → string
Writes amount of the unit unit, as MetricUnitFormatUtils.format would, without picking another unit. A
temperature is absolute on its own scale, so formatUnit("celsius", 20) is 20 °C.
MetricUnitFormatUtils.formatUnit("kilometerPerHour", 90) --> 90 km/h
MetricUnitFormatUtils.formatUnit("liter", 1, { unitDisplay = "long" }) --> 1 liter
MetricUnitFormatUtils.formatUnit("hours", 1.5) --> 1.5 hours
formatNumber
Writes a plain number for the locale: grouped thousands, the locale's decimal mark, and the digit options of MetricUnitFormatOptions.
MetricUnitFormatUtils.formatNumber(1234.5678) --> 1,234.568
MetricUnitFormatUtils.formatNumber(1234.5, { locale = "de-de" }) --> 1 234,5
MetricUnitFormatUtils.formatNumber(2, { minimumFractionDigits = 2 }) --> 2.00
getDecimalSeparator
MetricUnitFormatUtils.getDecimalSeparator(locale: string) → stringThe decimal mark locale writes numbers with, such as "." or ",".