How to get the double digits in a three digit number?

I am trying to split a 3 digit number to be 2 numbers.

I want 110 to become 1,10. I know you can do :split but(to my knowledge) that only works if its like 1:10.

Does anyone know how to accomplish this?

Are you trying to make a table with the two separations? Like {“1”,“10”}?

Yes, im trying to get the table like:

100 = table[1], 10 = table[2]

Do you want to always split it after the first number?

Yes. I want it to split after the first number.

Oh, in that case, this is what I would recommend:

local function numberToTable(num)
    local str = tostring(num)
    if string.len(str)==3 then -- makes sure it is a 3 digit number
        local hundreds = tonumber(string.sub(str,1,1).."00")
        local tens = tonumber(string.sub(str,2,3))
        return {hundreds,tens}
    end
end

print(numberToTable(110)[1]) -- prints 100
print(numberToTable(110)[2]) -- prints 10
1 Like
local n = 123
  
local hundreds = 100 * math.floor(n/100)
local tens = n % 100
  
print(hundreds, tens)

--> 100 23
local number = 123
local hundreds = math.floor(number % 1000 / 100) * 100
local tens = math.floor(number % 100 / 10) * 10
local ones = math.floor(number % 10)
print(number, "=", hundreds, "+", tens, "+", ones)

To construct an array for a number with any number of digits:

local number = 1234567
local digits = {}
for i = math.floor(math.log10(number)), 0, -1 do
	local n2 = 10 ^ i
	local n1 = n2 * 10
	table.insert(digits, math.floor(number % n1 / n2) * n2)
end

table.foreach(digits, print)

print(number, "=", table.concat(digits, " + "))
1 Like

Works perfectly with any number, thanks.
image

1 Like