Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Python's math module contains handy functions like floor & ceil. These functions take a   number and return the nearest integer below or above it. However, these functions return the answer as a floating-point number. For example:

import math 

f=math.floor(2.3)

Now f returns:

2.0

What is the safest way to get an integer out of this float, without running the risk of rounding errors (for example if the float is the equivalent of 1.99999) or perhaps I should use another function altogether?

1 Answer

0 votes
by (106k points)

You can use int(your non integer number) and it will give the correct result:-

print int(2.3) 

print int(math.sqrt(5)) 

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...