Hello everyone, I just did made something, and decided, “Why not show everyone how I did it?” Sometimes, a player’s stats in game can reach very high, and it may be quite hard to display numbers in instances such as UI or leaderstats. For instance, look at the image below.
As you can see, the top image looks much more cluttered than the bottom image, and with even more numbers, can be a UX disaster. So how would you be able to do this, you ask? Well, your answer is coding! So, we need a reference table to base our text “shortener”. We will use this as our table:
local NumeralDictionary =
{
K = 3, -- Thousand
M = 6, -- Million
B = 9, -- Billion
T = 12, -- Trillion
q = 15, -- Quadrillion
Q = 18, -- Quintillion
s = 21, -- Sextillion
S = 24, -- Septillion
O = 27, -- Octillion
N = 30, -- Nonillion
D = 33 -- Decillion+
}
Now, in the rare occasion that the stats may have decimals, we would need to make them into whole numbers:
local Number = 10000004.2
local ShortenedText = tostring(math.floor(Number + 0.5)) -- rounds to nearest whole number
So, how do we actually calculate if we need to shorten the text? Well, if the text has more than four characters, we will attempt to shorten the text. Then, we would loop across the dictionary, and see if the value in each index is equivalent or equal to the numbers of characters in the text, as well as when the value added by 3, it is greater than the # of characters in the text.
-- We can find the numbers of characters in a text by doing #Text , where "Text" is the text you are using
if #Text > 4 then
Text = Text:sub(1, #Text-4)
for Letter, Number in pairs(NumeralDictionary) do
if Number <= #Text and Number+3 > #Text then
if Number == #Text then Number = #Text-1 end
Text = Text:sub(1, #Text-Number) -- We get the digits that we need in order to shorten the text
Text = Text .. Letter .. "+" -- This will be the shortened version
break
end
end
end
So whenever we try it out, we will have the shortened version instead of the long version. Try it out!
local NumeralDictionary =
{
K = 3,
M = 6,
B = 9,
T = 12,
q = 15,
Q = 18,
s = 21,
S = 24,
O = 27,
N = 30,
D = 33
}
local Number = -- Your number here
local Text = tostring(math.floor(Number + 0.5)) -- rounds to nearest whole number
-- We can find the numbers of characters in a text by doing #Text , where "Text" is the text you are using
if #Text > 4 then
Text = Text:sub(1, #Text-4)
for Letter, Number in pairs(NumeralDictionary) do
if Number <= #Text and Number+3 > #Text then
if Number == #Text then Number = #Text-1 end
Text = Text:sub(1, #Text-Number) -- We get the digits that we need in order to shorten the text
Text = Text .. Letter .. "+" -- This will be the shortened version
break
end
end
end
print(Text)
**NOTE: If you are using stats that are very precise, or have a lot of decimals, this is not the tutorial for you. This can be used by anyone. Always use the original number to handle transactions, and not the "Text" string.**