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 Overflow Top answer 1 of 16
1382
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)))
2 of 16
348
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.
DataCamp
datacamp.com › tutorial › python-round-up
How to Round Up a Number in Python | DataCamp
July 22, 2024 - # Create a function to check if a decimal is exactly 0.5 and round up if true def round_half_up(n): if n - int(n) == 0.5: return int(n) + 1 return round(n) # Example usage example_number = 2.5 rounded_number = round_half_up(example_number) print(rounded_number) Round half to even, or bankers' rounding, is a rounding method used to minimize bias. When a number is exactly halfway between two integers, it is rounded to the nearest even integer. This method helps to balance the rounding process by avoiding a consistent upward or downward bias. If you omit the second argument, the built-in round method rounds half to even the numbers by default. # Python round() method to round half to even print(round(2.5)) # 2 print(round(3.5)) # 4 print(round(-2.5)) # -2
Videos
03:18
ROUNDING NUMBERS IN PYTHON: Simple way to round without rounding ...
30:27
Python - Rounding, Banker's Rounding (round, math.ceil, math.floor, ...
02:09
How To Round Numbers in Python - YouTube
04:03
Python Rounding of Numbers without the round, int functions - YouTube
Python Beginners Tutorial Rounding Up & Down Numbers 2022
03:56
Rounding Up (Video) – Real Python
Mimo
mimo.org › glossary › python › round-function
Python round(): Rounding Numbers in Python
If precision is critical, Python’s decimal module provides a way to avoid floating-point precision issues. The decimal module allows you to work with decimal numbers while avoiding the rounding errors of float numbers. ... from decimal import Decimal, ROUND_HALF_UP # Create Decimal objects instead of using floats num = Decimal('2.675') # Use the quantize() method to round to two decimal places with rounding mode ROUND_HALF_UP rounded_num = num.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) print(rounded_num) # Outputs: 2.68
Inspector
inspector.dev › home › round up numbers to integer in python – fast tips
Round Up Numbers to Integer in Python - Inspector.dev
June 17, 2025 - Bankers’ rounding is the default rounding method in Python. It rounds numbers with fractional parts that are exactly halfway between two integers to the nearest even integer. This method helps in reducing rounding biases. # Example 3: Round Half to Even (Bankers' Rounding) num1 = 2.5 num2 = 3.5 rounded_num1 = round(num1) rounded_num2 = round(num2) print(rounded_num1) # Output: 2 print(rounded_num2) # Output: 4
W3Schools
w3schools.com › python › ref_func_round.asp
Python round() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals.
Replit
replit.com › home › discover › how to round up in python
How to round up in Python
This is a common mistake when working with any Python module, not just math. Always ensure you've imported the necessary libraries before calling their functions to prevent this error from popping up. You'll get a TypeError if you give math.ceil() a value it can't handle, like a string. The function only works with real numbers, so passing it text will cause your script to fail. The following code snippet triggers this exact error. import math user_input = "3.7" rounded_up = math.ceil(user_input) # TypeError: must be real number, not str print(rounded_up)
GeeksforGeeks
geeksforgeeks.org › round-function-python
round() function in Python - GeeksforGeeks
import math num = 3.6 rounded_num = math.floor(num) # rounds down to nearest integer print(rounded_num) # output: 3 rounded_num = math.ceil(num) # rounds up to nearest integer print(rounded_num) # output: 4 ... In this example, we are using ...
Published August 7, 2024
Programiz
programiz.com › python-programming › methods › built-in › round
Python round()
print(round(num, 2)) # using decimal.Decimal (passed float as string for precision) num = Decimal('2.675')
Note.nkmk.me
note.nkmk.me › home › python
Round Up/Down Decimals in Python: math.floor, math.ceil | note.nkmk.me
January 15, 2024 - Convert a string to a number (int, float) in Python · print(int('10')) # 10 # print(int('10.123')) # ValueError: invalid literal for int() with base 10: '10.123' print(int('FF', 16)) # 255 ... Considering negative values, there are four ways to round up and down. ... print(math.floor(10.123)) # 10 print(math.floor(-10.123)) # -11 print(math.ceil(10.123)) # 11 print(math.ceil(-10.123)) # -10 print(int(10.123)) # 10 print(int(-10.123)) # -10 ... For example, you can define a custom function that rounds toward infinity as follows.
Hyperskill
hyperskill.org › university › python › rounding-and-round-in-python
Rounding and round() in Python
August 2, 2024 - If rounding to the whole number the round() function rounds up if the decimal part is 0.5 or greater and rounds down if its less, than 0.5. For instance rounding 3.2 using round(3.2) gives 3 as output; while rounding 3.5 returns 4 according to this function. The round() function, in Python ...