So I got a problem, I’m making a game where you can have money and all this stuff and when you have above 1M, it’s showing 1000000 and not 1M but I want 1M, 1B, 1T and everything above.
Can anyone help me?
So I got a problem, I’m making a game where you can have money and all this stuff and when you have above 1M, it’s showing 1000000 and not 1M but I want 1M, 1B, 1T and everything above.
Can anyone help me?
Could you not split up the string up and then see how many letters/numbers are in the string.
You can use this code:
local abbreviator = {}
-- variables
local abbreviations = {
["k"] = 4,
["m"] = 7,
["b"] = 10,
["t"] = 13,
["qd"] = 16,
["qi"] = 19,
["se"] = 22,
["sp"] = 25,
["o"] = 28,
}
-- functions
function abbreviator:Abbreviate(number)
local text = tostring(string.format("%.f",math.floor(number)))
local chosenAbbreviation
for abbreviation,digit in pairs(abbreviations) do
if (#text >= digit and #text < (digit + 3)) then
chosenAbbreviation = abbreviation
break
end
end
if (chosenAbbreviation) then
local digits = abbreviations[chosenAbbreviation]
local rounded = math.floor(number / 10 ^ (digits - 2)) * 10 ^ (digits - 2)
text = string.format("%.1f", rounded / 10 ^ (digits - 1)) .. chosenAbbreviation
else
text = number
end
return text
end
return abbreviator
(Code is in a module script)
Normally I wouldn’t give out code but this is extremely simple and you can follow this tutorial to understand (It’s really good): Abbreviate Numbers/Currency! - Roblox (Rounding and Formatting Strings) - YouTube
if you use values, use numbervalues instead of intvalues
That’s a bit lengthy just for a number formatter. I would find this one better by all means:
function formatNumber(amount)
local tableNo_ = tostring(amount):reverse():split("")
local str = ""
for i, c in ipairs(tableNo_) do
str ..= c
if i % 3 == 0 and i < #tableNo_ then
str ..= ","
end
end
return str:reverse()
end
OP was asking how to abbreviate numbers, not add commas to the zeros
oml I totally misread it, my bad