0
How do I round a number to the nearest whole in Python
4 Answers
+ 3
round(4.51) >>> 5
+ 1
You can also use string formatting to automatically round a float for you. To round to the nearest whole number use
:.0f
x = 4.6
print(f"{x:.0f}") # 5
0
Take note of the difference between python round and other programming languages.
See:
https://www.sololearn.com/post/876419/?ref=app
If you want it to work like the others, do this:
from math import floor
myRound = lambda x: floor(x + 0.5)
- 1
Maybe convert to integer int(4.675) --> 4