iiKossmoZ
(iiKossmoZ)
September 16, 2020, 11:14pm
#1
I’m looking at shortening Displays on my game, so rather than displaying stuff like: “1800” It’d show as 1.8k and so forth through the numbers.
I came across a tutorial on how to do it - Although, it doesn’t work the best {{ if at all as it’s registering ‘1800000’ as 1k+ }}.
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.
[image]
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 …
Thank you!
A solution that comes to my mind is to divide by 1000. With two possible outcomes:
· If the number is smaller than 1000, use that number and add “k”
· Else, if the number is greater than 1000, divide by 1000 again, and change “k” to “M”, and so on…
Example:
Number 1: 5500
Result: 5500 / 1000 = 5,5k
Number 2: 1650000
Result = 1650000 / 1000 = 1650K, not enough, divide by 1000 again… 1650 / 1000 = 1,65M
Hope it helps
posatta
(pasta)
September 16, 2020, 11:33pm
#3
Similar questions have been asked before, and there are some modules that do a pretty good job at this:
You can try converting it to a string and abbreviating it that way. A common module I’ve seen recommended is the one berezaa made for Miner’s Haven. Here’s his MoneyLib module:
(The relevant function is called “shorten”.)
This is an all-in-one number formatting module designed for displaying numbers in a more user-friendly way.
Why this module?
Aside from being the more known module (with over 100 likes), this is a solid tested module with many features included. Whether you insert a number large enough that some other number formatting module/snippets breaks, or you insert negative numbers or infinity, this module accounts for it.
As this is a module, you do not have to copy and paste any snippets, but you do…
2 Likes
Tomte_020
(ScriptedDev)
May 31, 2023, 4:06pm
#4
I hope this it what you’re looking for!
local function ShortenNumber(number)
if number >= 1000 then
return string.format(“%.0fK”, number / 1000)
elseif number >= 1000000 then
return string.format(“%.0fM”, number / 1000000)
end
end
print(ShortenNumber(200002)) – Output: 200K