Home > front end >  How to eliminate zero after decimal
How to eliminate zero after decimal

Time:09-17

I want to write a code to get the numbers which passes my calculation and gives output only if the said output is an integer. But the answers always get a 0 after the decimal point(208.0) and so I am unable to filter out the rest of false outputs which are floats. How can we print outputs with just a trailing zero and eliminate the rest or convert the float with just a trailing zero to integer so that it can be filtered out.

a = 200
while a<300:
 b=a (a*4.0/100)
 if type(b)==int:
  print a
 a=a 1

This code now prints nothing as there is no integer output

CodePudding user response:

You could try 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

Or try:

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

CodePudding user response:

Got the code from @U12-Forward ,shortened it a bit.

while a < 300:
    b = a   (a * 4.0 / 100)
    if b.is_integer():
        print a
        print int(b)
    a = a   1 

Now does the job!

  • Related