Skip to main content

MetricUnitSystem

A system of units: the units it declares, the units a value is shown in when it prefers this system, and the system it falls back to for everything else. Metric is the base: every unit is sized in SI units, and a value made from a metric unit prefers nothing. US customary, British imperial and Roblox studs are systems (MetricUnitSystems), and a game can declare its own, such as studs at another scale.

A value made from a system's unit shows itself in that system, so studs read back as studs. To show any value in a system, convert it with MetricUnit.ToSystem or ask the system to MetricUnitSystem.Format it.

local MINI = MetricUnitSystems.roblox({ name = "robloxMini", metersPerStud = 0.028 })

local height = MINI:New(5, "stud") -- 14 cm
MetricUnit.format(height) -- "5 studs"
MINI:In(height, "stud") -- 5
height:In("meter") -- 0.14

MetricUnitSystems.IMPERIAL:Format(MetricUnit.new(2, "liter")) -- "3.52 imp pt"

Every amount is held in SI units, so values made through different systems add, compare and convert freely.

Types​

MetricUnitSystemConfig​

interface MetricUnitSystemConfig {
name: string
units: {UnitDefinition}?
displayUnits: {{string}}?
pointDisplayUnits: {{string}}?
}

What a system is made from.

  • units are the units it declares, from MetricUnitDefinitions.createUnits. They take the place of a well-known unit of the same id, so a system can rescale stud and keep its names and translations.
  • displayUnits lists, for each dimension, the unit ids to choose from by size, largest first.
  • pointDisplayUnits does the same for absolute values, such as { "celsius" } for temperatures.
  • fallback is the system asked for any dimension this one does not list.

Functions​

new​

MetricUnitSystem.new(config: MetricUnitSystemConfig) → MetricUnitSystem

A system from config. See MetricUnitSystemConfig.

getSystemDeclaring​

MetricUnitSystem.getSystemDeclaring(unit: UnitDefinition) → MetricUnitSystem?

The system that declared unit in its config, or nil for a well-known unit.

isSystem​

MetricUnitSystem.isSystem(value: any) → boolean

Whether value is a MetricUnitSystem.

GetName​

MetricUnitSystem.GetName(self: MetricUnitSystem) → string

The system's name, such as "imperial".

GetFallback​

MetricUnitSystem.GetFallback(self: MetricUnitSystem) → MetricUnitSystem?

The system asked for the dimensions this one does not list, or nil.

GetUnits​

MetricUnitSystem.GetUnits(self: MetricUnitSystem) → {UnitDefinition}

The units this system declares itself, not counting its fallbacks.

GetUnit​

MetricUnitSystem.GetUnit() → UnitDefinition?

The unit unit names in this system: its own, then its fallbacks', then the well-known one. A unit given itself is returned as it is. The result can be passed anywhere a unit is named, and keeps this system's scale.

height:As(MINI:GetUnit("stud"))

GetDisplayUnits​

MetricUnitSystem.GetDisplayUnits(
dimensions: Dimensions,
absolute: boolean?
) → {UnitDefinition}?

The units a value of dimensions is shown in when it prefers this system, largest first: this system's, then its fallbacks', or nil when none lists the dimension. An absolute value takes the point display units first.

New​

MetricUnitSystem.New(
amount: number,
unit: UnitLike
) → MetricUnit

amount of this system's unit, such as MINI:New(5, "stud").

In​

MetricUnitSystem.In(
value: MetricUnit,
unit: UnitLike
) → number

How many of this system's unit value is, such as MINI:In(height, "stud").

Format​

MetricUnitSystem.Format() → string

Text for players with value in this system's units where it has them, as MetricUnit.format writes it.

MetricUnitSystems.IMPERIAL:Format(MetricUnit.kilograms(80)) -- "12.598 st"

Parse​

MetricUnitSystem.Parse(
text: string,
kind: (KindName | Dimensions)?,
options: ParseOptions?
) → (
string?--

why it could not be read

)

Reads text as MetricUnit.parse does, with this system's units: MINI:Parse("5 studs") is five of its studs.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "A system from `config`. See [MetricUnitSystemConfig].",
            "params": [
                {
                    "name": "config",
                    "desc": "",
                    "lua_type": "MetricUnitSystemConfig"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 89,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "getSystemDeclaring",
            "desc": "The system that declared `unit` in its config, or nil for a well-known unit.",
            "params": [
                {
                    "name": "unit",
                    "desc": "",
                    "lua_type": "UnitDefinition"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "MetricUnitSystem?"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 120,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "isSystem",
            "desc": "Whether `value` is a [MetricUnitSystem].",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 130,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "GetName",
            "desc": "The system's name, such as `\"imperial\"`.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 139,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "GetFallback",
            "desc": "The system asked for the dimensions this one does not list, or nil.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "MetricUnitSystem?"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 148,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "GetUnits",
            "desc": "The units this system declares itself, not counting its fallbacks.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ UnitDefinition }"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 157,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "GetUnit",
            "desc": "The unit `unit` names in this system: its own, then its fallbacks', then the well-known one. A unit given\nitself is returned as it is. The result can be passed anywhere a unit is named, and keeps this system's scale.\n\n```lua\nheight:As(MINI:GetUnit(\"stud\"))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                },
                {
                    "name": "unit",
                    "desc": "",
                    "lua_type": "UnitLike"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "UnitDefinition?"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 172,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "GetDisplayUnits",
            "desc": "The units a value of `dimensions` is shown in when it prefers this system, largest first: this system's, then\nits fallbacks', or nil when none lists the dimension. An absolute value takes the point display units first.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                },
                {
                    "name": "dimensions",
                    "desc": "",
                    "lua_type": "Dimensions"
                },
                {
                    "name": "absolute",
                    "desc": "",
                    "lua_type": "boolean?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ UnitDefinition }?"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 195,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "New",
            "desc": "`amount` of this system's `unit`, such as `MINI:New(5, \"stud\")`.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                },
                {
                    "name": "amount",
                    "desc": "",
                    "lua_type": "number"
                },
                {
                    "name": "unit",
                    "desc": "",
                    "lua_type": "UnitLike"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "MetricUnit"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 220,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "In",
            "desc": "How many of this system's `unit` `value` is, such as `MINI:In(height, \"stud\")`.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                },
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "MetricUnit"
                },
                {
                    "name": "unit",
                    "desc": "",
                    "lua_type": "UnitLike"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 232,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "Format",
            "desc": "Text for players with `value` in this system's units where it has them, as [MetricUnit.format] writes it.\n\n```lua\nMetricUnitSystems.IMPERIAL:Format(MetricUnit.kilograms(80)) -- \"12.598 st\"\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                },
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "MetricUnit | MetricUnitData"
                },
                {
                    "name": "options",
                    "desc": "",
                    "lua_type": "MetricUnitFormatOptions?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 247,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        },
        {
            "name": "Parse",
            "desc": "Reads `text` as [MetricUnit.parse] does, with this system's units: `MINI:Parse(\"5 studs\")` is five of its\nstuds.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MetricUnitSystem"
                },
                {
                    "name": "text",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "kind",
                    "desc": "",
                    "lua_type": "(KindName | Dimensions)?"
                },
                {
                    "name": "options",
                    "desc": "",
                    "lua_type": "ParseOptions?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "MetricUnit?"
                },
                {
                    "desc": "why it could not be read",
                    "lua_type": "string?"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 262,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "MetricUnitSystemConfig",
            "desc": "What a system is made from.\n\n- `units` are the units it declares, from [MetricUnitDefinitions.createUnits]. They take the place of a\n  well-known unit of the same id, so a system can rescale `stud` and keep its names and translations.\n- `displayUnits` lists, for each dimension, the unit ids to choose from by size, largest first.\n- `pointDisplayUnits` does the same for absolute values, such as `{ \"celsius\" }` for temperatures.\n- `fallback` is the system asked for any dimension this one does not list.",
            "fields": [
                {
                    "name": "name",
                    "lua_type": "string",
                    "desc": ""
                },
                {
                    "name": "units",
                    "lua_type": "{ UnitDefinition }?",
                    "desc": ""
                },
                {
                    "name": "displayUnits",
                    "lua_type": "{ { string } }?",
                    "desc": ""
                },
                {
                    "name": "pointDisplayUnits",
                    "lua_type": "{ { string } }?",
                    "desc": ""
                },
                {
                    "name": "fallback",
                    "lua_type": "MetricUnitSystem?",
                    "desc": ""
                }
            ],
            "source": {
                "line": 62,
                "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
            }
        }
    ],
    "name": "MetricUnitSystem",
    "desc": "A system of units: the units it declares, the units a value is shown in when it prefers this system, and the\nsystem it falls back to for everything else. Metric is the base: every unit is sized in SI units, and a value\nmade from a metric unit prefers nothing. US customary, British imperial and Roblox studs are systems\n([MetricUnitSystems]), and a game can declare its own, such as studs at another scale.\n\nA value made from a system's unit shows itself in that system, so studs read back as studs. To show any value\nin a system, convert it with [MetricUnit.ToSystem] or ask the system to [MetricUnitSystem.Format] it.\n\n```lua\nlocal MINI = MetricUnitSystems.roblox({ name = \"robloxMini\", metersPerStud = 0.028 })\n\nlocal height = MINI:New(5, \"stud\") -- 14 cm\nMetricUnit.format(height) -- \"5 studs\"\nMINI:In(height, \"stud\") -- 5\nheight:In(\"meter\") -- 0.14\n\nMetricUnitSystems.IMPERIAL:Format(MetricUnit.new(2, \"liter\")) -- \"3.52 imp pt\"\n```\n\nEvery amount is held in SI units, so values made through different systems add, compare and convert freely.",
    "source": {
        "line": 26,
        "path": "src/metricutils/src/Shared/Systems/MetricUnitSystem.lua"
    }
}