简体   繁体   中英

How to check last digit of number

Is there a way to get the last digit of a number. I am trying to find variables that end with "1" like 1,11,21,31,41,etc..

If I use a text variable I can simply put

print number[:-1] 

but it works for variables with text(like "hello) but not with numbers. With numbers I get this error:

TypeError: 'int' object is not subscriptable

I am trying to see if there's a better way to deal with numbers this way. I know a solution is to convert to a string and then do the above command but I'm trying to see if there's another way I have missed.

Thanks so much in advance...

Remainder when dividing by 10, as in

numericVariable % 10

This only works for positive numbers. -12%10 yields 8

Use the modulus operator with 10:

num = 11
if num % 10 == 1:
    print 'Whee!'

This gives the remainder when dividing by 10, which will always be the last digit (when the number is positive).

So you want to access the digits in a integer like elements in a list; easiest way I can think of is:

n = 56789
lastdigit = int(repr(n)[-1])
# >  9

Convert n into a string, accessing last element then use int constructor to convert back into integer.

For a Floating point number:

n = 179.123
fstr = repr(n)
signif_digits, fract_digits = fstr.split('.')
# >  ['179', '123']
signif_lastdigit = int(signif_digits[-1])
# >  9

I can't add a comment yet, but I wanted to iterate and expand on what Jim Garrison said

Remainder when dividing by 10, as in numericVariable % 10

This only works for positive numbers. -12%10 yields 8

While modulus (%) is working as intended, throw on an absolute value to use the modulus in this situation.

abs(numericVariable) % 10

Lostsoul, this should work:

    number = int(10)
#The variable number can also be a float or double, and I think it should still work.
lastDigit = int(repr(number)[-1])
#This gives the last digit of the variable "number." 
if lastDigit == 1 : 
    print("The number ends in 1!")

Instead of the print statement at the end, you can add code to do what you need to with numbers ending in 1.

Hope it helped!

Convert to string first:

oldint = 10101
newint = int(str(oldint)[-1:]) 

最简单和最有效的方法是使用提醒:

last_digit = orginal_number % 10

This is a simple yet effective way to do it

if number < 0:
       remainder = number % -10
else:
       remainder = number % 10

Try this efficient one-liner code to call the last digit of any integer. The logic is first to convert the given value to a string and then convert it into the list to call the last digit by calling the -1 index. After that, convert it into an integer to wrap it up.

val is a variable that represents any integer.

int(list(str(val))[-1])

Example:

val = 23442
int(list(str(val))[-1])`

Output: 2

By using iteration and the in built function of 'digit' the number is treated as binary and so it goes from backwards to forwards. Here is an example of a bit of code for you.

for digit in binary:
    denary= denary*2 + int(digit)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM