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

Viewed 564

How can I check weather or not the value after the decimal of an integer is a 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")
8 Answers

You can use the split method:

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")

Try this

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")

Simplest if condition

You can simply just extract the decimal part and check its value with if condition as mentioned below

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")

You can do this by using powers of 10, e.g. 1e17 for 17 decimals - 1e17 is slightly faster than 10**17. It can handle negative numbers and is bit overflow safe:

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

You can read more about divided integer and decimal here: https://stackoverflow.com/a/63868139/678611

You may try something like this

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

Something like int((a - int(a)) * 10) should do the trick:

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

you can try this

>>> 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")

If the denominator and the numerator are same, you can say the decimal place have zeros.

or you can compare a with the int(a), if it returns true, it means the decimals places are zero.


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