What this script does
Keep display formatting separate from your numeric economy and timer state.
- Best for
- Leaderboards, pet income labels, upgrade prices, timers, and result panels.
- Language
- Luau
- Version
- 1.0.0
- Validation
- Luau runtime tested
Official Luau 0.735 parsing and strict analysis pass, and the module is covered by the pure-module runtime behavior harness.
Included behavior
- Compact number suffixes
- Configurable decimal places
- Readable duration formatting
Install in the stated runtime
- 1Create a ModuleScript named Format.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Use the returned strings only for display and keep full-precision numbers in server state.
Minimal usage example
local Format = require(script.Parent.Format)
print(Format.compact(1_250_000, 1))
print(Format.duration(7_385))Example inputs and result
- Supply
- A numeric value and compact-display precision, or a duration in seconds.
- Expect
- Returns display strings for your UI, not changed game values.
These notes explain the example above. Read the complete source for every exported function and edge case before integrating it.
Complete source
No hidden import, minified block, or shortened loader.
--!strict
-- MIT License. Compact number and duration formatting for your own UI.
local Format = {}
local suffixes = { "", "K", "M", "B", "T", "Qa", "Qi" }
function Format.compact(value: number, decimals: number?): string
local sign = if value < 0 then "-" else ""
local absolute = math.abs(value)
local tier = 1
while absolute >= 1000 and tier < #suffixes do absolute /= 1000; tier += 1 end
local places = math.clamp(math.floor(decimals or 1), 0, 3)
local scale = 10 ^ places
local rounded = math.floor(absolute * scale + 0.5) / scale
return sign .. tostring(rounded) .. suffixes[tier]
end
function Format.duration(seconds: number): string
local total = math.max(0, math.floor(seconds))
local hours = math.floor(total / 3600)
local minutes = math.floor(total % 3600 / 60)
local remainder = total % 60
if hours > 0 then return string.format("%dh %02dm %02ds", hours, minutes, remainder) end
return string.format("%dm %02ds", minutes, remainder)
end
return table.freeze(Format)
006487be2fa78b0aae10e6895b51611119bf8ee80ba3ecf3b6133027d20d75daOutside this script’s scope
- Change underlying numeric values
- Localize currency or dates
Found a problem or a better approach?
Tell us what failed, what you expected, and whether the issue happened in the Luau CLI or Roblox Studio. Suggestions for clearer types, safer server checks, or a more useful API are welcome.