What is the method in Python to round up a number?

In Python, you can use the ceil() function from the math module to perform ceiling rounding, which returns the smallest integer that is not less than x.

First, you need to import the math module.

import math

Next, you can use the ceil() function to round up the number.

x = 3.2
y = math.ceil(x)
print(y)  # 输出4

Additionally, you can use the built-in round() function to achieve rounding up, but you need to add a very small positive number to the decimal to achieve rounding up.

x = 3.2
y = round(x + 0.1)
print(y)  # 输出4
bannerAds