整数的最后2位数? Python 3 [英] Last 2 digits of an integer? Python 3
本文介绍了整数的最后2位数? Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用我的代码,我想得到一个整数的最后两位数字。但是当我创建xa正数时,它将取前x个数字,如果是负数,它将删除前x个数字。
With my code, I want to get the last two digits of an integer. But when I make x a positive number, it will take the first x digits, if it is a negative number, it will remove the first x digits.
代码:
number_of_numbers = 1
num = 9
while number_of_numbers <= 100:
done = False
num = num*10
num = num+1
while done == False:
num_last = int(repr(num)[x])
if num_last%14 == 0:
number_of_numbers = number_of_numbers + 1
done = True
else:
num = num + 1
print(num)
推荐答案
为什么不提取数模100的绝对值?也就是说,使用
Why don't you extract the absolute value of the number modulus 100? That is, use
abs(num) % 100
提取最后两位数?
在性能和清晰度方面,这种方法很难被击败。
In terms of performance and clarity, this method is hard to beat.
这篇关于整数的最后2位数? Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文