Converting a 1,000 to a digit numeral?

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+ }}.

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 :slight_smile:

Similar questions have been asked before, and there are some modules that do a pretty good job at this:

2 Likes

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