-3

I'm trying to create a module that will separate the decimal from a number as I need it for a project. However when I test it the integer comes out fine but the decimal always gets an error:


def seperate(decimal):
    integer = int(decimal)
    dec = decimal-integer
    print(dec, integer)

        

if I say try to enter 2015.677 it give this: 0.6769999999999072 2015

what is wrong?

2 Answers2

2

Please check the imageActually what i suggest is that you can try this alternative approach, And for this to work properly we always need to pass a floating value to it, and it also works for your conditions

def seperate(decimal):
    integer = int(decimal)
    decimal = str(decimal)
    dec = decimal[decimal.index('.')+1:]
    return(int(dec), integer)
SARAN SURYA
  • 534
  • 5
  • 14
-2

what's the error you are getting?

I tried your code, and it worked just fine on Python 3.7

decimal = 3.5
integer = int(decimal)
dec = decimal-integer
print(dec, integer)

If you give us the error, perhaps I can help you a little bit more.

Regards!

Edit:

After reading the update on the rounding problem, i used the "round" function and returned the 0.677 part of your decimal just fine.

Andrés
  • 13
  • 5