Variables and Data Types Practice Problems - DECIMAL PART

answer / proposition :

variable x is available to you

x = 81.3242434534

#make x a string
y = str(x)

#remove the integer part of the float variable and round it up , up to the length of the string
decimal = round(x- int(x),len(y))

1 Like

Please provide the screen link and if it’s a question, change your topic tag into “Q & A” and specify the question.

Welcome to the community!


No need to convert to string type and then round up.

Just this would work

decimal = x - int(x)

for instance :

x = 17.890980

decimal = x - int(x)

decimalfloat (<class ‘float’>)

0.890979999999999 —> not precise

My proposition reflects the decimal part accurately

Okay than try after correcting length y from your OP code


y = str(x).split('.')[1]     # Calculate length of precision only

So it would be

x = 17.890980
y = str(x).split('.')[1]
decimal = round(x - int(x), len(y))


I think this will work fine. :slight_smile:

1 Like

sure ok ! string format upgraded :+1:

1 Like