简体   繁体   English

如何查看号码的最后一位

[英]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..我试图找到以“1”结尾的变量,如 1、11、21、31、41 等。

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:但它适用于带有文本的变量(如“hello”)但不适用于数字。使用数字我会收到此错误:

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除以 10 时的余数,如

numericVariable % 10

This only works for positive numbers.这仅适用于正数。 -12%10 yields 8 -12%10 产量 8

Use the modulus operator with 10:使用带 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).这给出了除以 10 时的余数,这将始终是最后一位数字(当数字为正数时)。

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.n转换为字符串,访问最后一个元素,然后使用 int 构造函数将其转换回整数。

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我还不能添加评论,但我想重复并扩展 Jim Garrison 所说的内容

Remainder when dividing by 10, as in numericVariable % 10除以 10 时的余数,如 numericVariable % 10

This only works for positive numbers.这仅适用于正数。 -12%10 yields 8 -12%10 产量 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: Lostsoul,这应该工作:

    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.您可以添加代码以使用以 1 结尾的数字来执行所需的操作,而不是末尾的 print 语句。

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.逻辑是先把给定的值转成字符串,再转成list调用-1索引调用最后一位。 After that, convert it into an integer to wrap it up.之后,将其转换为整数以将其包裹起来。

val is a variable that represents any integer. val是表示任何整数的变量。

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

Example:例子:

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

Output: 2输出: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在数字后附加校验位 - How to append a check digit to a number 如何检查给定的数字是否为数字? - How to check the given number is digit or not? 如何获取 4 位数字的所有可能组合的列表,其单个数字总和为 13,最后一位数字为 5 - how get list of all the possible combinations of 4 digit numbers whose individual digit sum is 13 and last digit is 5 in that number 如何检查一个数字的每个数字是否大于或等于另一个数字? - How to check every digit of a number is greater or equal than another number? 最后一位数为5的数字的舍入 - Roundoff of a number having 5 as last digit 查找十进制数的最后一位 - Finding the last digit of decimal number 如何在 pandas 中的数字系列中获取每一行的最后一位数字 - How to get the last digit in each row in a number series in pandas Function 其中(第一个数字的最后一位)*(第二个数字的最后一位)=(第三个数字的最后一位)[PYTHON] - Function Where (Last Digit of first Number)*(last digit of second number)=(Last digit of Third Number) [PYTHON] Python:自动为4位数字添加校验位 - Python: automatically add a check digit to a 4 digit number 如果列表中包含列表中的最后一个数字,如何删除列表中的最后一个数字 - how to remove the last digit in a list if the list contain number last number some were in the list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM