Experimental

An early preview: docs, the API and the dashboard may change or break.

Report a problem
Module

@std/format

Numbers and times as players read them: compact amounts, grouped digits, durations, clocks and countdowns.

local format = require("@std/format")

Functions

format.clock(seconds: number) -> string
#

Seconds as a countdown clock: "4:05", or "1:04:05" from an hour; fractions cut, negatives are 0.

local format = require("@std/format")
local players = require("@ember/players")
local ui = require("@ember/ui")

-- Seconds as a clock: 245 as "4:05". `duration` gives "4m 5s" instead.
task.spawn(function()
	for left = 180, 0, -1 do
		ui.actionBar(players.all(), `<yellow>Time left: {format.clock(left)}`)
		task.wait(1)
	end
end)
format.compact(n: number, digits: number?) -> string
#

1999 as "1.9K", 2.5e6 as "2.5M", up to "Vg" at 10^63. Rounds toward zero, so a player never sees more than they have; digits fraction digits (default 1), trailing zeros dropped. Below 1000 the number shows as is: compact(250) is "250". NaN shows as "NaN".

local data = require("@ember/data")
local format = require("@std/format")
local players = require("@ember/players")

-- Big numbers, short: 1250 as "1.2K", 3400000 as "3.4M". It rounds down, so a player never sees
-- more than they have. `grouped` writes the full number with commas instead.
local profile = data.player({ cash = 0 })

players.joined:connect(function(player)
	local cash = profile:get(player).cash
	player:send(`<green>${format.compact(cash)} <gray>({format.grouped(cash)} exactly)`)
end)
format.countdown(epoch: number, options: { style: ("clock" | "duration")?, done: string? }?) -> string
#

A MiniMessage tag the host draws as the time left until epoch (seconds, as os.time and task.epoch give), redrawn each second wherever it is shown: a sidebar, entity text, a HUD. The time left rounds up, so the last second shows 0:01; from epoch on it shows done (MiniMessage, default 0:00 or 0s). style is "clock" (4:05, the default) or "duration" (4m 5s).

local format = require("@std/format")
local players = require("@ember/players")
local ui = require("@ember/ui")

-- A countdown the host redraws every second, with no script loop: `countdown` returns a tag for a
-- moment in `task.epoch` time, and it ticks down wherever it is shown.
local endsAt = task.epoch() + 300

local board = ui.sidebar({
	title = "<red><bold>Escape",
	lines = { `<gray>Doors close in {format.countdown(endsAt, { done = "<red>closed" })}` },
})

players.joined:connect(function(player)
	board:show(player)
end)
format.duration(seconds: number) -> string
#

Seconds as "45s", "5m", "5m 30s", "2h" or "2h 15m"; fractions cut, negatives are 0.

format.grouped(n: number, digits: number?) -> string
#

1234567.891 as "1,234,567", or with digits fraction digits (default 0), cut not rounded.