Search code examples
lua

Getting a specific number from a bigger number?


a = (any random number)
Is there a way could I get the third number (12345) without converting it into a string?
If not, what would be a good way to get it by converting it into a string?


Solution

  • You can use the sub function

    function getDigit(value,digitPlace) 
    return tonumber(tostring(value):sub(digitPlace,digitPlace))
    end
    

    This will get the third digit of a as a number:

    a = 12345
    print(getDigit(a,3))