Home > other >  How to print the fractional part of the number?
How to print the fractional part of the number?

Time:07-14

It's my first question So, the problem is python rounding. I have seen it, but I dont really know how to get around it. For example: i have the number 10.34 - I need to receive just fractional part, so 0.34 I had some ideas how to do that. One of this:

n = float(input())
print(n - int(n))

In case of 10.34 the code give me "0.33999999999999986" instead 0.34. I have some ideas how to do it with help of strings or another tools, but the task assumes that I need just some basic tools

CodePudding user response:

Use round:

res = n - int(n)
print(round(res, 10))

CodePudding user response:

n = float(input()) n = n - int(n) n = round(n,2)

https://www.w3schools.com/python/ref_func_round.asp The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals.

round(number, digits)

For your refrence

  • Related