如何检查整数小数点后的值是否为零

How can I check whether or not the value after the decimal of an integer is equal to zero

如何检查天气是否为整数小数点后的值为零

a = 17.3678

if a???:
  print("the value after the decimal point is zero")
else:
  print("the value after the decimal point is not zero")

您可以尝试这样的操作

if str(a).split(".")[1]=="0":

您可以使用拆分方法:

a = 17.3678
a = str(a)
valueAfterPoint = a.split('.')[1]
valueAfterPoint = int(valueAfterPoint)

if valueAfterPoint == 0:
  print("the value after the decimal point is zero")
else:
  print("the value after the decimal point is not zero")

int((a - int(a)) * 10) 这样的东西应该可以解决问题:

>>> a = 17.3678
>>> print(int((a - int(a)) * 10))
3
>>> a = 17.03678
>>> print(int((a - int(a)) * 10))
0

试试这个

a = 17.3678

if a.is_integer():
   print("the value after the decimal point is zero")
else:
   print("the value after the decimal point is not zero")

你可以试试这个

>>> a = 17.3678

>>>if a/float( int(a) ) == 1:
          print("the value after the decimal point is zero")
   else:
          print("the value after the decimal point is not zero")

如果分母和分子相同,可以说小数点有零。

或者你可以比较a和int(a),如果returns为真,则表示小数位为零。

最简单的if条件

您可以简单地提取小数部分并使用 if condition 检查其值,如下所述

a = 17.3678

if a%1==0:
    print("the value after the decimal point is zero")
else:
    print("the value after the decimal point is not zero")

if num%1 == 0:
    print("Value after the decimal point is zero")
else:
    print("Value after the decimal point is not zero")

您可以使用 10 的幂来做到这一点,例如17 位小数的 1e17 - 1e17 比 10**17 稍快。它可以处理负数并且位溢出安全:

>>> x=3.0
>>> x*1e17 - int(x)*1e17 == 0
True
>>> x=3.333
>>> x*1e17 - int(x)*1e17 == 0
False

您可以在此处阅读有关除整数和小数的更多信息: