MetricUnitDefinitions
Every unit MetricUnit can be made in, read in, parsed from and written in. This list is where a unit is declared: its id, dimensions, size in SI base units, symbol, measurement system, English names and the other text it may be typed as. MetricUnit, MetricUnitFormatUtils, MetricUnitParseUtils and MetricUnitFallbackStrings all read it. Symbols are the SI ones (or the customary ones for US and imperial units), which do not change with the locale.
To add a unit:
-
Add a row to the
definecall for its dimension and system below. See UnitRow for the fields. Give an SI unitprefixesrather than adding its prefixed forms:kilo,nanoand the rest are made from it. -
Add its id to the
UnitIdunion, with any prefixed forms games commonly name. luau-lsp rejects a row whose id is missing from it; other prefixed ids work at runtime. - Add it to a system's display units in MetricUnitSystems if it should be picked by size.
-
If it is written by name, add
units.<id>.long.<category>andunits.<id>.short.<category>for all six plural categories to everyLocalization/MetricUnitTranslator/<locale>.json.txt: English from this row, the other languages from CLDR's unit patterns. Prefixed forms need no entry; they are written from theprefixessection and the unit's own entry. - Only for a unit games construct constantly, add a named constructor on MetricUnit.
The specs catch a named unit without English names or translation keys, and a unit that does not round-trip through MetricUnit.new and MetricUnit.In.
Types
UnitId
type UnitId = "microsecond" | "millisecond" | "second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year" | "perSecond" | "perMinute" | "perHour" | "perDay" | "perWeek" | "perMonth" | "perYear" | "hertz" | "percent" | "permille" | "millimeter" | "centimeter" | "meter" | "kilometer" | "inch" | "foot" | "yard" | "mile" | "stud" | "squareMillimeter" | "squareCentimeter" | "squareMeter" | "hectare" | "squareKilometer" | "squareFoot" | "acre" | "milliliter" | "liter" | "cubicMeter" | "usFluidOunce" | "usGallon" | "imperialGallon" | "bit" | "kilobit" | "megabit" | "gigabit" | "byte" | "kilobyte" | "megabyte" | "gigabyte" | "kibibyte" | "mebibyte" | "gibibyte" | "bitPerSecond" | "kilobitPerSecond" | "megabitPerSecond" | "gigabitPerSecond" | "bytePerSecond" | "kilobytePerSecond" | "milligram" | "gram" | "kilogram" | "tonne" | "ounce" | "pound" | "stone" | "robloxMassUnit" | "meterPerSecond" | "kilometerPerHour" | "milePerHour" | "knot" | "studPerSecond" | "meterPerSecondSquared" | "studPerSecondSquared" | "newton" | "kilonewton" | "meganewton" | "poundForce" | "joule" | "kilojoule" | "megajoule" | "kilowattHour" | "kilocalorie" | "watt" | "kilowatt" | "megawatt" | "pascal" | "kilopascal" | "megapascal" | "bar" | "poundForcePerSquareInch" | "kilogramPerCubicMeter" | "gramPerCubicCentimeter" | "kelvin" | "celsius" | "fahrenheit" | "radian" | "degree" | "revolution" | "radianPerSecond" | "degreePerSecond" | "revolutionPerMinute" | "radianPerSecondSquared" | "microampere" | "milliampere" | "ampere" | "coulomb" | "millivolt" | "volt" | "kilovolt" | "ohm" | "kilohm" | "megohm" | "siemens" | "picofarad" | "nanofarad" | "microfarad" | "farad" | "microhenry" | "millihenry" | "henry" | "weber" | "millitesla" | "tesla" | "nanometer" | "micrometer" | "centiliter" | "deciliter" | "terabyte" | "petabyte" | "microgram" | "milliwatt" | "gigawatt" | "kilohertz" | "megahertz" | "gigahertz" | "becquerel" | "mole" | "millimole" | "micromole" | "kilomole" | "gramPerMole" | "molePerLiter" | "millimolePerLiter" | "katal" | "candela" | "steradian" | "lumen" | "lux" | "kilolux" | "gray" | "milligray" | "sievert" | "millisievert" | "microsievert" | "nauticalMile" | "literPer100Kilometers" | "squareInch" | "squareYard" | "squareMile" | "cubicInch" | "cubicFoot" | "cubicYard" | "usTeaspoon" | "usTablespoon" | "usCup" | "usPint" | "usQuart" | "imperialFluidOunce" | "imperialPint" | "imperialQuart" | "shortTon" | "footPerSecond" | "footPerSecondSquared" | "newtonMeter" | "britishThermalUnit" | "poundFoot" | "metricHorsepower" | "horsepower" | "inchOfMercury" | "poundPerCubicFoot" | "kilometerPerLiter" | "milePerUsGallon" | "milePerImperialGallon" | "rankine"A unit's id: the singular, camelCase name of the unit. Every id has an entry in MetricUnitDefinitions; reading, formatting and MetricUnit.new all take it, so a typo is a type error.
UnitLike
A unit id, any of @quenty/time's names for a time unit, such as "hours" or "h", or a unit itself, such as
MINI:GetUnit("stud") from a MetricUnitSystem of the game's own scale.
UnitFamily
type UnitFamily = string
The name of the MetricUnitSystem that declares a unit: "metric", "usCustomary", "imperial",
"usCustomaryAndImperial" for the units the United States and Britain share (the inch, mile, pound, mph, BTU,
horsepower), "roblox", or a game's own system. Time units and bare rates belong to none, since every system
shares them.
UnitForms
interface UnitForms {one: stringother: string}A singular and plural form of a unit's English name or phrase.
UnitDefinition
interface UnitDefinition {id: UnitIddimensions: Dimensionsfactor: numbersymbol: stringsystem: UnitFamily?names: UnitForms?shortTemplates: UnitForms?aliases: {string}?zero: number?per: UnitId?duration: boolean?prefix: UnitPrefixOf?}One unit.
-
factoris its size in SI base units: seconds, metres, kilograms, bytes, kelvin, radians, amperes, moles or candelas, or their product for a derived unit, so a newton is 1 (kg·m/s²) and a kilowatt-hour 3,600,000 (J). -
namesis its American English name, for units written by name. Commonwealth spelling (metre, litre, tonne) is derived. -
shortTemplatesreplaces the default short phrase, the symbol after a space, where that reads wrongly (45%,45°,1 stud).%sis the number. durationmarks the time units, which durations are written in.prefixis set on a unit made from a prefix and another unit, such askilometer.
The other fields are as in UnitRow.
UnitPrefixOf
For a unit made by prefixing another, the prefix's name and the unit it prefixes: kilometer is kilo and
meter. A translator writes it from those two when it has no entry for the unit itself.
UnitRow
interface UnitRow {id: UnitIdfactor: numbersymbol: stringname: string?plural: string?aliases: {string}?zero: number?per: UnitId?prefixes: PrefixSet?elidesPrefixVowel: boolean?}One row of the list below, for a unit of the kind ("length", "speed") and system its define call gives.
factoris its size in SI base units.-
nameis its American English singular, for units written by name;pluraldefaults tonameplus "s". Time units are written with@quenty/time's words and bare rates with per phrases, so they have none. shortreplaces the symbol-after-a-space short phrase, as one template or a singular and plural.aliasesis other text it may be typed as when parsed, such as"kph".-
zerois where the unit's scale starts, in base units. A unit with one is a scale whose readings are points: MetricUnit.new makes absolute values in it. Only the temperature scales have one. peris the time unit a bare rate such asperHourcounts per. Hertz is a rate too, but is written by name.-
prefixesmakes a unit for each prefix of a [PrefixSet]:meterwith"si"makeskilometer,nanometerand the rest, with names, symbols and aliases from the unit's. A unit declared outright keeps its own row. elidesPrefixVoweldrops a prefix's final vowel before the unit, as SI writeskilohmandmegohm.
Prefix
interface Prefix {name: stringsymbol: stringfactor: number}A metric or binary prefix: kilo is k and 1000.
PrefixSet
type PrefixSet = "si" | "siMultiples" | "siMultiplesAndBinary"
Which prefixes a unit takes: "si" all twenty-four SI prefixes, "siMultiples" only those of 10 and above (no
millibytes), and "siMultiplesAndBinary" those and the binary ones (KiB, MiB).
Properties
ROBLOX_METERS_PER_STUD
MetricUnitDefinitions.ROBLOX_METERS_PER_STUD: numberMetres in a Roblox stud, as Roblox documents it against the default avatar: 28 cm.
Functions
getUnit
The unit named unit, or nil. Takes any UnitLike, so "hours" and "h" find "hour", and a unit is
returned as it is.
getPrefixedUnit
The unit prefix makes of base, such as kilometer for kilo and meter, or nil when base does not take
that prefix.
getDeclaredUnits
The units declared outright, without their prefixed forms, in the order declared. Cheaper than MetricUnitDefinitions.getUnits; MetricUnitDefinitions.getPrefixedUnit finds the rest.
getUnits
Every unit, in the order declared, then every prefixed form. Building the prefixed forms costs a few milliseconds the first time, so look units up with MetricUnitDefinitions.getUnit where you can.
getUnitIds
The ids of every unit of dimensions, in the order declared.
getPrefixes
Every prefix a unit can take: the twenty-four SI prefixes, smallest first, then the binary ones.
getTimeAliases
@quenty/time's other names for its time units, such as "h" for "hour", mapped to unit ids.
isUnitDefinition
MetricUnitDefinitions.isUnitDefinition(value: any) → booleanWhether value is a UnitDefinition, such as one MetricUnitSystem.GetUnit returns.
createUnits
Units for a MetricUnitSystem of a game's own, from rows of one kind as in UnitRow, with their prefixed forms. They are not added to the well-known units; only a system holding them knows them.
local units = MetricUnitDefinitions.createUnits("length", "space", {
{ id = "lightYear", factor = 9.4607e15, symbol = "ly", name = "light-year" },
})
createRobloxUnits
MetricUnitDefinitions.createRobloxUnits(system: string,--
the name of the system that declares them
metersPerStud: number) → {UnitDefinition}
The Roblox units at a stud of metersPerStud: stud, studPerSecond, studPerSecondSquared and
robloxMassUnit, a cubic stud of water, so it scales with the cube of the stud.