2

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")
Abhi
  • 1,080
  • 1
  • 7
  • 21
jennyflysky
  • 43
  • 1
  • 7
  • 3
    To avoid confusion and add clarity I think you should change your question to: "How can I check **whether** or not the value after the decimal of an integer is **equal to zero** – Liam Mar 06 '22 at 19:10

8 Answers8

4

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")
Abhi
  • 1,080
  • 1
  • 7
  • 21
3

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

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")
Tom
  • 486
  • 1
  • 2
  • 11
  • I'm picking this as the correct number as it also works for numbers such as 18.0001234 where many other answers would fail to notice this. Thankyou – jennyflysky Mar 06 '22 at 18:15
2

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.

2

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

Diblo Dk
  • 585
  • 10
  • 26
1

You may try something like this

if str(a).split(".")[1]=="0":
Python learner
  • 1,159
  • 1
  • 8
  • 20
1

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
Joshua Robles
  • 151
  • 1
  • 8
1

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