, asked
Views : 11

How do you round up a float number in Python?

How do you round up a float number in Python?                                            

1 Answer
Jayashree

Python has an in-built function round() for this purpose. The function takes two arguments, the number to be rounded and the places upto which it is to be rounded. If the number is to be rounded to nearest integer, the second argument is not given.

>>> round(1.7456)
2
>>> round(1.4756)
1

If however, it is to be rounded up, 0.5 is added to it before rounding

>>> round(1.7456+0.5)
2
>>> round(1.4756+0.5)
2

Advertisements
Related Questions