Money counter with International system of units

Hello!

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)

Just make a getDisplayValue function:


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

[updated to be more complete]

But what happens when it is 2000000? It will return 1M instead of 2M

You’ll need another function for that :slight_smile:

function NumberOps.getFirstDigit(number)
	local numStr = tostring(number)
	local firstDigit = tonumber(numStr:sub(1, 1))
	return firstDigit
end

Updated first example to be more complete.

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 :slight_smile:

1 Like

How would you do something like that? I mean, What would a math genius do? I really want to know :laughing:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.