Search code examples
pythonint

error when trying to separate a integer from a decimal


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?


Solution

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