Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to add something like 'K' or 'M' for numbers that are large?

How do you add 'K' or 'M' after those large numbers?

1 comment

0

string concatenation

1 answer

✓ Accepted answer

You would do two things:

  • Use tostring() to convert the number to a string.
  • Use the length operator (#) to get the length of the string, and depending on how big the number converted to a string is, apply the letter accordingly. (You would also use string.sub() to get the number to concatenate the letter with, which happens to be either the first, first 2 or first 3 digits of the number.)

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).

1 comment

0

Correction: They are in separate conditionals with separate scopes, so the local variables are nil there.

This discussion is preserved as it was. New answers, comments, and votes are closed.