I made a money counter text label. It works, but I want to convert numbers like 1,000 to 1K or 1,000,000 to 1M.
When the money number is too large the money counter’s letters are really small. That makes it unintelligible. And I have NO IDEA how to do this. So I would like an explanation.
I saw some posts but I don’t understand them.
There isn’t issues with my code. I just want to update it to the International system of units.
task.wait(0.1)
local Player: Player = script.Parent.Parent.Parent.Parent
local Cash : IntValue = Player:WaitForChild("Cash")
local MoneyCounter = script.Parent
Cash:GetPropertyChangedSignal("Value"):Connect(function()
local StringCash = tostring(Cash.Value)
MoneyCounter.Text = StringCash
end)
local function getFirstDigit(number): number | nil
local numStr = tostring(number)
local firstDigit = tonumber(numStr:sub(1, 1))
return firstDigit
end
local function getDisplayValue(val: number): string
if val >= 1000000 and val < 9999999 then
return tostring(getFirstDigit(val))..'M'
elseif val >= 1000 and val < 9999 then
return tostring(getFirstDigit(val))..'K'
else
return tostring(val)
end
end
Obviously this is still not “complete” and in general I’d call this a “brute force” method. There is likely a more elegant solution out there that a math wizard created but I’ve made a nice career on using brute force instead of being a math wizard