+ 1
How do I to round float result?
I Will calculate 10 / 3, and result is 3.333333333 I want To round this result into 3 number, so The result should be 3.333 So, how I do it?
3 Antworten
+ 2
if you want to print this result, rounding float to certain decimal points, do this:
x == 10/3
print("{0:.3f}".format(x))
that would output your number limiting it to 3 signs after a dot
+ 1
def round(num):
if (num + 0.5) % 2 == 0:
print (num + 0.5)
elif (num + 0.4) % 2 == 0:
print(num + 0.4)
elif (num + 0.3) % 2 == 0:
print (num + 0.3)
elif (num + 0.2) % 2 == 0:
print (num + 0.2)
elif (num + 0.1) % 2 == 0:
print (num + 0.1)
else:
print(int(num))
round(5.3)
round(5.5)
This works but is a little enefficient.
Output:
5
6
+ 1
x = 10/3.0
round (x , 3)
# Or in one step
round (10/3.0 , 3)