How do I get the last digit of a number?

Is there a way to get a certain digit out of a number?

For instance, if I had the number 673048648, is there any way that I could figure out that the ones column is ‘8’ and the tens column is ‘4’?

1 Like

Modulus should work fine for this.

local t = 42738476237
local LastDigit = math.floor(t%10)
print(t) -- 42738476237
print(LastDigit) -- 7
8 Likes

A manipulative method

Modulus is cool and all with finding remainders or last debit solutions, but with the string manipulation method, you can get any digit in the number. Also, it looks way cooler. (I guess that is just opinion though)

Getting the last digit

local num =12345678
local numS = tostring(num)
local last = tonumber(string.sub(numS, string.len(numS)-1, 0)
print(last) --8

Getting other digits

local function getLoc(str, num)
    return string.sub(str, num-1,num)
end
local num = 12345678
local place = 2 --1 is ones, 2 is tenths, 3 is hundreds
local numS = tostring(num)
local result = getLoc(numS, string.len(numS)-place+1)
--result is 7
3 Likes

Hey, you really don’t need to edit your answer to put other people’s answers into it.

1 Like

Sorry, I was just trying to explain that method better. I removed it from my post.

1 Like

Not sure why you would use string manipulation with numbers, you can get other digits with modulus.

local t = 42738476237
local Digit = math.floor(t%100/10)
print(t) -- 42738476237
print(Digit) -- 3

As a function

local function getDigit(number,position)
	return math.floor(number%10^position/(10^position/10))
end

Using the function

local t = 12345678.98
print(getDigit(t,1)) -- 8
print(getDigit(t,2)) -- 7
print(getDigit(t,3)) -- 6
print(getDigit(t,4)) -- 5
print(getDigit(t,5)) -- 4
print(getDigit(t,6)) -- 3
print(getDigit(t,7)) -- 2
print(getDigit(t,8)) -- 1
--and for decimals
print(getDigit(t,0)) -- 9
print(getDigit(t,-1)) -- 8
9 Likes

Both ways are equally effective; It just depends which way you prefer and how you want to think about it.

1 Like

Would you also be working with decimal numbers? If so, you can use:

string.sub((n..''):reverse(),1,1)

Otherwise, use:

n%10

Arithmetic functions are always faster than string manipulations. Since this is a function that could be used in performance critical situations, there is no reason to use the slower approach.

How much faster though? If there’s only a few miliseconds shaved off, time difference doesn’t matter much. Regardless, it certainly looks pointless to convert a number to a string, perform a string manipulation and then turn it back into a number.

1 Like

Not by an enormous margin on individual runs, but this is what I got:

The code
local strsub = string.sub
local tonumber = tonumber
local tostring = tostring
local tick = tick
local print = print
local function getDigitN(number,position)
     local d = 10^position
     local n = number%d/(d/10)
     return n-n%1
end
local function getDigitS(number,position)
     number = tostring(number):reverse()
     return tonumber(strsub(number,position,position))
end
local n,dig = 1234567890,5
wait(2)

local st = tick()
for i=1,999999 do
     getDigitN(n,dig)
end
print("Arithmetics: "..tick()-st)
wait(5)

local st = tick()
for i=1,999999 do
     getDigitS(n,dig)
end
print("Strings: "..tick()-st)

The result was 0.16 for the arithmetic version VS 0.86 for string manipulation, so the difference is quite noticeable if you have to run it many times.

I couldn’t run more as this PC crashes if a program holds for 2 seconds, but the results were about the same when I reversed the order.

2 Likes