Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)
edited by

I was searching for a function which, given a float, will restore an int of the float amazed or adjusted to the closest int. Is there something like this built-in or accessible in a module? 

The accompanying code gets the job done however I try not to waste time.

import math

def realround(number):

    _, d = divmod(number, 1)

    if d > 0.5:

        return int(math.ceil(number))

    else:

        return int(math.floor(number))

print(realround(12.3))

print(realround(14.5))

print(realround(15.8))

1 Answer

0 votes
by (26.4k points)
edited by

It's called as round:

print(round(12.3))

print(round(14.5))

print(round(15.8))

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

Watch this video tutorial for more information.

Browse Categories

...