How to find out the number in a specific position of an int variable

Is there a string.sub-like command for int variables?

Let’s say I need to find the second character in the number 1328 (which is 3).

1 Like

not sure, but couldn’t you just use tostring() and then tonumber()?

local inputString = tostring(number)
local result = tonumber(string.sub(inputString, tostring(searchNumber)))
if result then
    print(result)
end
1 Like

Of course I can, but I asked about such a command because I wanted less writing in my code, and in this case there is even more of it than I have right now.

1 Like

You do not have to convert int variable to string in order to use string.sub()

string.sub(), however, will return a string value. But it will not be a problem because whenever you try to perform any mathematical operations with it, Luau will automatically try to convert it into a number.

1 Like

I do not know a way to make it shorter

print(tonumber(tostring(Value):sub(Pos,Pos)))
2 Likes

Then: local d = math.floor(n / 10^(#tostring(n) - 2)) % 10

1 Like

why not just make new function then?

1 Like

Thanks for your answers, but I just needed to know that such a command exists. Your solutions can’t help because I didn’t provide the context.

And by the way, this information is useful, so let me mark it as a solution. :slight_smile: