The math.ceil (ceiling) function returns the smallest integer higher or equal to x.
For Python 3:
import math
print(math.ceil(4.2))
For Python 2:
import math
print(int(math.ceil(4.2)))
Answer from Steve Tjoa on Stack OverflowThe math.ceil (ceiling) function returns the smallest integer higher or equal to x.
For Python 3:
import math
print(math.ceil(4.2))
For Python 2:
import math
print(int(math.ceil(4.2)))
I know this answer is for a question from a while back, but if you don't want to import math and you just want to round up, this works for me.
>>> int(21 / 5)
4
>>> int(21 / 5) + (21 % 5 > 0)
5
The first part becomes 4 and the second part evaluates to "True" if there is a remainder, which in addition True = 1; False = 0. So if there is no remainder, then it stays the same integer, but if there is a remainder it adds 1.
How do you round up in Python?
Round half up Error
Using round_half_up to round numbers
Question: how to round In python?
Here's the code
Here's the output
Essentially, I'm asking is how do I make the numbers round up to look like the expected output.
I don't want to round to the nearest even number, I just want to always round my halve up.
I just want to do something like:
import decimal print(round_half_up(2.5))
But I've figured out that's not right. How would I do that?
Edit:
If anyone is curious why. I'm graphing some data I rounded. The even numbers are clearly bigger than the odd numbers