Decimal For Currency GUI

Hello Developers!
A bit of a backstory before I go into my issue (so everything makes sense).

I was trying to see what happened in my GUI when I collected a coin that would have given like 2t coins. It didn’t let me collect it because that amount of numbers could not fit in that single GUI. So I changed the code so if the player gets 1M+ coins, it would say “1.00M+”.

My issue is, I cannot figure out how to make it so it says the exact decimal. For example, if someone had 1,234,567 coins, I want the GUI to say “1.23M+”. (Same goes for billion, trillion, and on.")

Here is the code i am using so far:

local player = game.Players.LocalPlayer
while wait() do
	
	script.Parent.Text = player.leaderstats.Coins.Value
	
	if player.leaderstats.Coins.Value >= 1000000 then
		script.Parent.Text = "1.00M+"
	end
end

If anyone could help me with this it would be great! Thanks!
Im still learning so sorry if this is something very simple to do.

2 Likes

You can use string formatting to specify the number of decimals. If you have a static list of orders of magnitude you want to use (ex. if you only ever want to use million, billion, and trillion and not cover cases that go any higher) you can use conditionals:

function formatCoins(amount)
   local order = nil
   if amount < math.pow(10,6) then --1 million
      return tostring(amount)
   elseif amount < math.pow(10, 9) then --1 billion
      return string.format("%.2fM+", amount/math.pow(10,6))
   elseif amount < math.pow(10,12) then --1 trillion
      return string.format("%.2fB+", amount/math.pow(10,9))
   else
      return string.format("%.2fT+", amount/math.pow(10,12))
   end
end

More on string formatting: Formatting and Converting Strings

2 Likes

This will go inside of your text lable.

local abbreviations = {"", "K", "M", "B", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc", "Un", "Duo", "Tre", "Qua", "Qui", "Sed", "Sep", "Oct", "Nov", "V"}
-- You can remove or add if you feel
-- 100Qa, 700Oc, 999Sep, etc.

local function Format(value, idp)
	local ex = math.floor(math.log(math.max(1, math.abs(value)),1000))
	local abbrevs = abbreviations [1 + ex] or ("e+"..ex)
	local normal = math.floor(value * ((10 ^ idp) / (1000 ^ ex))) / (10 ^ idp)

	return  ("%."..idp.."f%s"):format(normal, abbrevs)
	
end

while wait() do
	local player = game.Players.LocalPlayer
	script.Parent.Text = Format(player.leaderstats.Coins.Value, 2)
	-- Change the "Defense" to the name of your leaderstat Name not variable
end
3 Likes

Thank you both, that really helps, I wish I could give 2 solutions but I can’t so I’ll give it to Astro because his script made it work, lol. But again thank you both for the lesson, and I’ll definitely look into that link on formatting and converting strings!
sorry didnt mean to solution myself

1 Like

ok, sorry for the bother, but when i first tried the script everything worked, it worked for thousands too (thanks lol) but now it isnt working for those numbers at all, only 1m and up. Is there anything I can add to make it work?

nevermind, it works at certain times, but not if someone buys something…weird

Edit: lol sorry, it works if someone buys something, but everything under 1M doesnt work when the player rejoins

What do you mean if someone buys something? Like from a in game shop or?

Like a developer product that gives coins or gems. Or spending coins purchasing something.

BTW the 2 that he passed in the second parameter of the function is what tells it to have 2 numbers after the decimal, if you want more or less than just tweak the number.

Make the player variable and coins variable outside the loop then instead of using a while loop use the coins.Changed event

you cant do infinite abbreviations theres a limit to it

He doesn’t have infinite abbreviations. I have more in my script for a SPTS game

in that script it was

local abbreviations = {"", "K", "M", "B", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc", "Un", "Duo", "Tre", "Qua", "Qui", "Sed", "Sep", "Oct", "Nov", "V"}
-- You can remove or add if you feel
-- 100Qa, 700Oc, 999Sep, etc.

which is saying you can just keep on adding more but it only stops at 1e+308

You can, especially if you are using BugNum or either Number and not IntValued

Thats not as easy as you think it is

It is. I have more abbreviations then this in my script.

I dont believe that, it took me a month to get bignum to start working in one of my test games

Oh, okay, thank you for that. 2 was perfect though. Sorry for the late response

No problem, I just wanted to let you know so you know how to customize it (also I always use 2). I always use this script, idk if that guy got it from the devforum post I used or a YouTube video (such as mine on this topic)