How do you add 'K' or 'M' after those large numbers?
You would do two things:
Here's an example:
local num = 10000 local str = tostring(num) -- The number as a string if #str > 3 and #str <= 6 then -- Checks if the number is within the thousands and is said as if it were within the thousands if #str == 4 then local newstr = string.sub(str, 1, 1) local numstr = newstr .. "K+" elseif #str == 5 then -- The number I used fits this conditional as it has 5 digits local newstr = string.sub(str, 1, 2) local numstr = newstr .. "K+" elseif #str == 6 then -- The last possible thousand local newstr = string.sub(str, 1, 3) local numstr = newstr .. "K+" end end
If you're familiar with the length operator for tables (#), it also returns the length of a string. Because what it returns is a number, you can check for the length of this number using comparison symbols.
If you take another look at the code, the variable appears to be constantly reassigned but it actually isn't. The modified string is always a new variable because the local variables newstr
and numstr
are constantly created (they are nil outside of where they are defined, as they are always in the highest scope).