How to Round a Number Up in Python?

To round a number up in Python, import the math library with import math, and call math.ceil(number). The function returns the ceiling of the specified number that is defined as the smallest integer greater than or equal to number

The following code shows how to round the number 42.42 up to 43.

>>> import math
>>> math.ceil(42.42)
43

If you don’t want to import the math module, you can use the following one-liner beauty:

x = int(input('your number: '))
rounded_up = int(x) + (int(x)!=x)
  • The int() built-in function cuts of the decimal part, i.e., rounds down.
  • The expression int(x)!=x evaluates to 1 if the decimal part of x is greater than 0. Otherwise, it becomes 0.
  • This helps us because only if the decimal part is greater than 0, we need to add +1 to the rounded-down number to round it up.

Rounding Up After Division

If the float to be rounded up comes from a division operation a/b, you can also use integer division a//b to round down to the next integer, and increment this by one. Thus, the expression a//b+1 rounds the resulting number up if a is not divisible by b, otherwise, the result of a//b would already provide the “rounded-up” semantics.

You can create a simple ternary operator x if y else z to differentiate between those two conditions:

a = int(input('a='))
b = int(input('b='))

rounded_up = a//b + 1 if a%b else a//b
print(rounded_up)

The code goes through the following steps:

  • Get the input strings from the user using the built-in input() function.
  • Convert the inputs to integer values using the built-in int() function.
  • Use the modulo operation a%b to differentiate between b being a divisor of a or not.
  • If not, the result will have a remainder and you can use integer division a//b to round down and increment this by one.
  • If yes, the result won’t have a remainder and you can simply use integer division because it, mathematically, would already be considered to be rounded up.
  • You use the ternary operator to pack this logic into a single line of code.

Here’s an example execution that was rounded up:

a=8
b=3
3

And here’s an example execution that wasn’t:

a=8
b=4
2

An alternative one-liner to round up two integers would be the following beauty:

a = int(input('a='))
b = int(input('b='))

rounded_up = a // b + (a % b > 0)
print(rounded_up)

The expression (a % b > 0) evaluates to True if b is not a divisor of a, otherwise it evaluates to False. As the Boolean True is represented by the integer value 1 in Python and Boolean False by the integer value 0 in Python, the expression increments only if b is not a divisor of a.

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!