How to Round Up a Number in Python?

Rounding-off numbers are commonly used to make the calculation easier to understand. Several functions, such as round(), are used to round up or down the floating point to the nearest integers. But to specifically round up a number to the nearest integers, the math.ceil(), numpy.ceil(), etc., are used in Python.

This post provides multiple methods to round up a number in Python using the below contents:

  • Method 1: Using the math.ceil() Function
  • Method 2: Using the numpy.ceil() Function
  • Method 3: Using the decimal Module
  • Method 4: Using the int() Function | For Positive Numbers
  • Method 5: Using the round() Function

Method 1: Using math.ceil() Function 

The “math.ceil()” function is utilized to round the number up in Python. The below code uses this function to demonstrate this method:

Code:

import math
print(math.ceil(3.1))
print(math.ceil(3.5))
print(math.ceil(3.7))
print(math.ceil(-3.4))
  • The module named “math” is imported.
  • The “math.ceil()” function takes a floating point number as an argument and retrieves the nearest integer by rounding up the number.

Output:

Floating point numbers have been rounded to the nearest integer.

Method 2: Using numpy.ceil() Function

In Python, the “numpy.ceil()” function is also utilized to round up a number. Here is an example code:

Code:

import numpy
print(numpy.ceil(3.7))
print(numpy.ceil(3.5))
print(numpy.ceil(3.3))
print(numpy.ceil(-3.3))
  • The module named “numpy” is imported.
  • The “numpy.ceil()” takes the floating point number as a parameter and retrieves the nearest integer by rounding up the number.

Output:

The numbers have rounded up to the nearest integers.

Method 3: Using decimal Module

A decimal floating point calculation can be performed quickly and correctly with the decimal module. The following code is used in the “decimal” module to round up a number:

Code:

import decimal
num = 3.7
print(decimal.Decimal(str(num)).quantize(decimal.Decimal('1.'), decimal.ROUND_CEILING))
num1 = 3.3
print(decimal.Decimal(str(num1)).quantize(decimal.Decimal('1.'), decimal.ROUND_CEILING))
  • The module named “decimal” is imported.
  • The Decimal() function converts the float num to a decimal number.
  • The quantize() method is then called on the decimal number with the precision set to “1.” and the rounding mode set to ROUND_CEILING.

Output:

Integers have been rounded up to the nearest whole number.

Method 4: Using the int() Function | For Positive Numbers

The below code uses the “int()” function to convert the float number into int and round up the number to the nearest integers:

Code:

num = 3.3
print(int(num) + ((int(num) - num) != 0))

num = 3.5
print(int(num) + ((int(num) - num) != 0))

num = 3.8
print(int(num) + ((int(num) - num) != 0))
  • The int() function is used to get the integer part of the number and discards the decimal part.
  • The integer value is added by 1 if the distance between the integer and the original float number is not zero.
  • In this case, the difference between 3 and 3.3 is not equal to zero, so it adds 1 to 3, and the final output is 4. 

Output:

The given floating numbers have been rounded up.

Method 5: Using the round() Function 

The “round()” function is utilized to round the input number up if the decimal value is equal and greater than ”.5”. But the round() function rounds down the number to the nearest integers if the value is lesser than “.5” (which is not included in our case). 

To round up a Number using this method, the following conditions must be fulfilled:

  • For Positive Numbers: the decimal part must be greater than or equal to 5. 
  • For Negative Numbers: the floating point must be less than 5. 

In the below example, the “round()” function is utilized to round the given number up to the nearest integers:

Code:

print(round(3.8))
print(round(3.5))
print(round(-3.4))

The round function takes the floating point number and rounds the number up to the nearest integers.

Output:

Rounding has been done to the nearest integer for the input numbers.

Conclusion

In Python, the “math.ceil()” function, “numpy.ceil()” function, “decimal module,” “int() function,” and “round()” function are used to round up a number. The “math.ceil()” function and “numpy.ceil()” function is the most simple and efficient method to round up a number to the nearest integers. The “int()” function method can only apply to positive numbers, and the “round()” method can also have limitations and conditions to round up numbers in Python. Rounding up a number in Python has been explained in depth in this Python tutorial.