Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Shortening how currency is displayed?

The title may not make sense but what I mean is how would I change this 100000 to 100,000 or 100K on a UI element.

What might work

One way I thought I could do it was get the length of the number by using string.len() but I am not sure how to place the comma (or remove the second half and replace it with K, M, or B etc..) into the currency.

Example:

currencyLength = string.len(tostring(currencyExample.Value)) 

End

I could use string.sub() for indexing correct character and either add a comma or remove half and add a character but I am not sure how to do it.

if string.len(tostring(currency.Value)) == 6 then
	string.sub(tostring(currency.Value), 3)
	-- then shorten (this would just return 100 if currency had a value of 100000)
end

I saw this sort of thing in the zed nov tycoon kit where you can convert the currency from 1000 to 1K. I have looked through the scripts and have not found anything there though except a function that I can not find called ConvertShort.

Thanks for the help!

2 comments

0

Instead of using string.len, use the length operator "#". There's no need for string.len.

0

ok

1 answer

✓ Accepted answer

I'm not sure where you wanted the display to be but I'm assuming that you want the "CashToCollect" display to be displaying "100B" if the players have "100,000,000,000" cash.

If that's what you wanted, then here is the script for it!

Firstly, you need a ModuleScript in ReplicatedStorage. Name it to whatever you like but I name mine "Moneh".

In your ModuleScript, type/copy this code:

local MoneyLib = {}

MoneyLib.Suffixes = {"k","M","B","T"}

MoneyLib.HandleMoney = function(Input)
	local Negative = Input < 0
	Input = math.abs(Input)

	local Paired = false
	for i,v in pairs(MoneyLib.Suffixes) do
		if not (Input >= 10^(3*i)) then
			Input = Input / 10^(3*(i-1))
			local isComplex = (string.find(tostring(Input),".") and string.sub(tostring(Input),4,4) ~= ".")
			Input = string.sub(tostring(Input),1,(isComplex and 4) or 3) .. (MoneyLib.Suffixes[i-1] or "")
			Paired = true
			break;
		end
	end
	if not Paired then
		local Rounded = math.floor(Input)
		Input = tostring(Rounded)
	end

	if Negative then
		return "(-$"..Input..")"
	end
	return "$"..Input
end

Once you have that done then, in your "CashToCollect" display Gui change the script into this:

local moneylib = require(game.ReplicatedStorage.Moneh)

script.Parent.Text = "$"..script.Parent.Parent.Parent.Parent.Parent.Parent.CurrencyToCollect.Value

script.Parent.Parent.Parent.Parent.Parent.Parent.CurrencyToCollect.Changed:Connect(function(money)
	script.Parent.Text = moneylib.HandleMoney(money)
end)

Oh by the way, if you are dealing with more then 32 bit integers then use "NumberValue" instead. I believe that this is it. If this helps you in any way and it works(which it should) then, accepting this answer would be great! :P Thanks!

2 comments

0

Thanks!

0

I will test this and if it works then will accept the answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.