Kryzeth Standard Library

StdLib.Data.Energy

Utilities for parsing, formatting, and scaling Factorio Energy strings.

Methods3

function Energy.parse(energy: string) -> (value nil | number, unit "W" | "J" | nil, prefix nil | string)

Parses a Factorio 'Energy' string into its base-unit value. For example, "1.5MW" returns 1500000, "W", "M". Returns 'nil' when the value is not a valid energy string.

Parameters
energystring
Returns
valuenil | number

Value expressed in plain watts or joules

unit"W" | "J" | nil
prefixnil | string

Original SI prefix

function Energy.format(value: number, unit: EnergyUnit, prefix: nil | string) -> energy nil | string

Formats a base-unit energy value as a Factorio 'Energy' string. When prefix is omitted, the largest suitable Factorio multiplier is used. For example, Energy.format(1500000, "W") returns "1.5MW".

Parameters
valuenumber

Value expressed in plain watts or joules

prefixnil | string

SI prefix to force; defaults to automatic selection

Returns
energynil | string
function Energy.scale(energy: string, scalar: number) -> scaled_energy nil | string

Multiplies a Factorio 'Energy' string while retaining its original prefix. For example, Energy.scale("60kW", 1.5) returns "90kW". Returns 'nil' when 'energy' is invalid or 'scalar' is not a finite number.