我想编写一个代码来获取通过我的计算的数字,并且仅当所述输出是整数时才给出输出。但是答案总是在小数点(208.0)后得到 0,所以我无法过滤掉其余的浮点假输出。我们如何打印仅带有尾随零的输出并消除其余部分或将仅带有尾随零的浮点数转换为整数,以便将其过滤掉。
a = 200
while a<300:
b=a+(a*4.0/100)
if type(b)==int:
print a
a=a+1
此代码现在不打印任何内容,因为没有整数输出
你可以试试float.is_integer
:
a = 200
while a < 300:
b = a + (a * 4.0 / 100)
if b.is_integer():
print int(b)
else:
print b
if type(b) == int:
print a
a = a + 1
或尝试:
a = 200
while a < 300:
b = a + (a * 4.0 / 100)
if int(b) == b:
print int(b)
else:
print b
if type(b) == int:
print a
a = a + 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句