How can I turn a number to a shorter number (I don't know how to explain, click to understand :3)

Hey devs!

In my game, I have currencies like in all games.
There are GUIs wich show the currencies value but, if the player has, idk something like 30000000 coins,
how can I turn it to “30,000,000” or “30M”?

It’s actually quite simple, here’s Berezza’s method for abbreviating numerals ( Provides a lot more options than @sjr04 mentioned );

local Suffixes = {"k","M","B","T","qd","Qn","sx","Sp","O","N","de","Ud","DD","tdD","qdD","QnD","sxD","SpD","OcD","NvD","Vgn","UVg","DVg","TVg","qtV","QnV","SeV","SPG","OVG","NVG","TGN","UTG","DTG","tsTG","qtTG","QnTG","ssTG","SpTG","OcTG","NoTG","QdDR","uQDR","dQDR","tQDR","qdQDR","QnQDR","sxQDR","SpQDR","OQDDr","NQDDr","qQGNT","uQGNT","dQGNT","tQGNT","qdQGNT","QnQGNT","sxQGNT","SpQGNT", "OQQGNT","NQQGNT","SXGNTL"}                                              

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

	local Paired = false
	for i,v in pairs(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) .. (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 -- returns 1.0k for example
end

Now to add commas to a numeral , or to abbreviate it that style is a different function;


function AbbreviateNumeral(numeral)
	if numeral then
		local left,num,right = string.match(numeral,'^([^%d]*%d)(%d*)(.-)$')
		return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right -- returns for example 1,000, it gets every 3 zeros and adds a  comma
	end
end

All you have to do is call one of these functions and it’ll return a number abbreviated!

4 Likes

So when I call the AbbreviateNumeral function I just have to send the currency’s value as an argument?

Yeah that’s literally all you have to do, for example;

Text = AbbreviateNumeral(10000) --the text will be 10,000
Text = Convert(10000) --the text will be 10k
1 Like

Thx you for your help!
You make this hard thin (MATHS x) ) So easy to understand!
Thank your really much for your help!
Mhh what do you think it is the best to do?
Wich option the plr will prefere? :confused:

1 Like

In my game that is upcoming, Samurai Legends, it’s easier for players to understand Berezza’s conversion it seems to be, but whichever you prefer and think is cleaner is the one you should go with! :smiley:

Mhh, Berezza’s is the first or the second one? x)
If i’m right it is the first one.

The Berezza method is the first funtion provided ( Convert ) , it can also support negative numerals, which is pretty cool.

Mhh, Do you have a few more minutes, I have some questions to ask you (sorry i’m new in scripting and i want to learn as much as possible !

Shoot me a PM here or on Discord, Kensizo#0001 , we cannot fill the replies section and constantly bump this post, this is considered spam.