How to round up a number in python?

Member

by craig , in category: Python , 2 years ago

How to round up a number in python?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by raul_reichert , 2 years ago

@craig If you want to round up a number in Python you can probably want to use a math.ceil() method in Python, let's see how it works:


1
2
3
4
5
import math

number = 8.2
# Output: 9
print(math.ceil(number))


by kyla.kuvalis , a year ago

@craig 

In Python, you can round up a number using the ceil() function from the math module. Here is an example:

1
2
3
4
5
6
import math

x = 3.14159
rounded_up = math.ceil(x)

print(rounded_up)  # Output: 4


In the above example, we imported the math module, which provides a range of mathematical functions. We then called the ceil() function on the number x, which rounded it up to the nearest integer. Finally, we printed the rounded up value.


Note that the ceil() function always rounds up to the nearest integer, regardless of the decimal value.