On Windows 7 Python 3.2, the following:
print(int(math.ceil(24/10)))
gives me '3' as expected.
On Windows Server with Active Python 2.5, it gives me '2'.
What is the issue here and how can I solve it?
Here's my original code:
number_of_pages = int(math.ceil(number_of_rows/number_of_rows_per_page))
Thanks,
Barry
The Python ceil() function rounds a number up to the nearest integer, or whole number. Python floor() rounds decimals down to the nearest whole number. Both of these functions are part of the math Python library.
The math. ceil() method rounds a number UP to the nearest integer, if necessary, and returns the result.
There are floating point numbers that do not fit into an integer, so the function would fail if it returned an integer. Returning the same type as the parameter ensures the result will fit.
Description. This function returns a floating-point value representing the nearest whole number that is greater than or equal to the value passed to it. If that value is an integer it will, by definition, be the value math. ceil() returns, albeit as float not an integer.
Python 2.x uses truncating disivion, so the answer to 24/10
is 2. The ceil
of 2 is still 2.
The fix is to convert one of the operands to float:
print(int(math.ceil(24.0/10)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With