Skip to content

Instantly share code, notes, and snippets.

@Xevion
Created July 20, 2024 00:07
Show Gist options
  • Save Xevion/aa387daa1140f9ef9e9a399147d7a286 to your computer and use it in GitHub Desktop.
Save Xevion/aa387daa1140f9ef9e9a399147d7a286 to your computer and use it in GitHub Desktop.
function shortnumberstring(number)
local steps = {
{1,""},
{1e3,"k"},
{1e6,"m"},
{1e9,"g"},
{1e12,"t"},
}
for _,b in ipairs(steps) do
if b[1] <= number+1 then
steps.use = _
end
end
local result = string.format("%.1f", number / steps[steps.use][1])
if tonumber(result) >= 1e3 and steps.use < #steps then
steps.use = steps.use + 1
result = string.format("%.1f", tonumber(result) / 1e3)
end
return result .. steps[steps.use][2]
end
function RPG_format_number(number)
local result = 0
if number then
if number < 1000000000 then result=format_number(number)
else result=shortnumberstring(number) end
end
return result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment