Python round up practical examples [5 Methods]


Python

Author: Bashir Alam
Reviewer: Deepak Prasad

 

How to round up number in Python - Introduction

The process of replacing a number by another number of approximately the same value but having fewer digits is known as rounding in Mathematics. If we round the number to an integer value slightly larger then the original number is known as rounding up. In this tutorial, we will learn about Python round up.

We will discuss various methods to round up a floating number by solving various examples. In general, we will cover 4 ways of rounding up a float number in Python. In a nutshell, this tutorial will contain all the necessary and popular methods that are used in Python to round up a floating number.

 

Method-1: Using Python round() function to round up a number in Python

Rounding numbers make them simpler and easier to use. Although they're slightly less accurate, their values are still relatively close to what they originally were. In Python, we have a built-in method known as round(), which is used to round the number to the nearest integer value. See the example below:

# defining floating numbers
num1 = 3.4
num2 = 3.6
# applying round method
print("Num1 is rounded to", round(num1))
print("Num2 is rounded to ", round(num2))

Output:

Num1 is rounded to 3
Num2 is rounded to 4

Notice that num1 is rounded to 3 which is a value slightly less than the original value, such type of rounding is known as round down. While num2 is rounded to 4 which is slightly larger than the original value and such type of rounding is known as round up. For example, see the example below:

5.5 Round up to 6
7.7 Round up to 8
3.1 Round up to 4

In Python, we have rounded the floating numbers to the nearest larger integer values. In the upcoming sections, we will discuss the Python round up which means to round a number to the nearest larger integer.

 

Example-1 Python round up to 2 decimal digits

There can be many methods to round a number up to 2 decimal digits in Python. For example one of the methods is to use the floating number method and specify the number of digits that we want. See the example below:

# floating number
num = 3.786847638
# Python round up number
print("The number round up to 2 decimal digits is: {0:.2f}".format(num))

Output:

The number round up to 2 decimal digits is: 3.79

Notice that in the above example, we have to round a number to 2 decimal digits. Another way to do this task is to use the round method and specify the number of decimal digits. For example, see the example below:

# floating number
num = 3.786847638
# Python round up number
print("The number round up to 2 decimal digits is: " ,round(num, 2) )

Output:

The number round up to 2 decimal digits is: 3.79

In the above example, we have specified the decimal digits by passing 2 as an argument to the round method.

 

Example-2 Python round up to nearest 10

There are two different ways to round up a number of the nearest 10. The first way is to use the round method. We can divide the value by 10, round the result to zero precision, and multiply with 10 again. The second way is to pass a negative value for precision. The negative denotes that rounding happens to the left of the decimal point. See the example below:

# defining a number
num = 457583
# Python round up number to nearest 10
rounded = round(num/10)*10
# print
print("The number round up to nearest 10 is : " ,rounded)

Output:

The number round up to nearest 10 is : 457580

Notice that we have successfully rounded the number to the nearest 10. The second way is to pass a negative value as an argument to the round method. See the example below:

# defining a number
num = 457583
# Python round up number to nearest 10
print("The number round up to nearest 10 is : " ,round(num, -1))

Output:

The number round up to nearest 10 is : 457580

Providing a negative value as an argument to the round method will round the number to the nearest 10 as shown in the above example.

 

Example-3 Python round up to the nearest integer

We already have discussed and covered various methods to round up a number to the nearest integer. Mostly, the round method is used to round a number to the nearest integer. If we will not specify the number of decimal points in the argument, the round method will round the number to the nearest integer value. See the example below:

# defining a number
num = 4.57583
# Python round up number to nearest integer value
print("The number round up to nearest integer is : " ,round(num))

Output:

The number round up to nearest integer is : 5

Notice that we did not provide the second argument to the round method so it automatically rounded the number to the nearest integer value.

 

Example-4 Python round up to nearest 5

Rounding a number to a multiple of a base returns the multiple nearest to that number. For example, rounding 11 to a multiple of 5 results in 10. In this section, we will round a number to the nearest 5. See the python program below:

# defining a number
num = 4343
# Python round up number to nearest 5
rounded = 5 * round(num/5)
# printing
print("The number round up to nearest 5 is : " ,rounded)

Output:

The number round up to nearest 5 is : 4345

Notice that in the example above, first, we divide the number with 5 and then round it to the nearest integer value by using the round method. Then we multiply the value by 5.

 

Example-5 Python round up division

We can use any of the above-mentioned roundup methods to round a number in a division. We can apply division and then round the result using any of the Python rounding methods. Here we will use the python round method and math.ceil method to round the number. See the example below:

import math
# defining a number
num1 = 43.0
num2 = 10
# Python round up division
print("Rounded using round method:", round(num1/num2))
print("Rounded using math.ceil method: ", math.ceil(num1/num2))

Output:

Rounded using round method: 4
Rounded using math.ceil method: 5

As we already discussed the ceil method round up the number to the nearest integer value larger or equal to the given number, that is why we get 5 instead of 4.

 

Example-6 Python round up float number

So far we have applied all the rounding methods on the floating number. Apart from those methods, we can also round up floating methods by specifying the number of digits that we want to have. See the example below:

# defining a number
num1 = 43.947386493
# Python round up floating number
print("Rounded to 5 decimal digits: {0:.5f}".format(num1))
print("Rounded to 0 decimal digits: {0:.0f}".format(num1))

Output:

Rounded to 5 decimal digits: 43.94739
Rounded to 0 decimal digits: 44

This is the simplest way to round up a floating number. All we need to do is to specify the number of decimal digits.

 

Example-6 Python round up numpy

The numpy round method is a mathematical function that rounds an array to the given number of decimals. For example, see the python program below, which uses the NumPy round method.

# importing numpy module
import numpy as np
# defining numpy array
Array = [1.1, 2.6, 4.6, 4.8, 10]
# printing array without rounding
print("Without rounding values: ", Array)
# python round up and printing
print ("Rounded values  : ", np.round(Array))

Output:

Without rounding values: [1.1, 2.6, 4.6, 4.8, 10]
Rounded values : [ 1. 3. 5. 5. 10.]

Notice that the NumPy round method returns the values of the array rounded to nearest integer values.

 

Method-2: Using math.ceil() function to round up a number in Python

The  math.ceil() function is a mathematical function that returns the ceiling value of the given number. Ceiling value means the nearest integer value larger than or equal to the original number. You can read more about the ceiling function from the article Python ceiling function. In this section, we will just learn the basic syntax and will solve some examples to round up a number using the python ceiling function.

 

Syntax of Python math.ceil() function

Before using the ceil function, we have to first import the math module using the import keyword. The following is the simple syntax of math.ceil() method.

import math
math.ceil(number)

The ceil method takes one argument ( floating or integer value) and returns the ceiling or the nearest larger or equal integer value.

 

Example of math.ceil() method to find the Python round up of a number

Now let us take some examples and see how we can use the math.ceil() method to find the round up of a number. See the example below:

# importing the math module
import math
# defining numbers
num1 = 3.1
num2 = 3.8
num3 = 4
# Python round up using ceil method
print("Round up value of num1 is :", math.ceil(num1))
print("Round up value of num2 is :", math.ceil(num2))
print("Round up value of num3 is :", math.ceil(num3))

Output:

Round up value of num1 is : 4
Round up value of num2 is : 4
Round up value of num3 is : 4

Notice that the floating values in the above examples were rounded to the nearest larger integer value while an integer value as an argument was not changed. The number returned by the math.ceil() method is int type. See the example below:

# importing math module
import math
num1 = 3.3
# python round up using math ceil method
num = math.ceil(num1)
# printing the type
print("The type of the number is :", type(num))

Output:

The type of the number is : <class 'int'>

Notice that the number returned by the ceil method belongs to the int class.

 

Method-3: Using Numpy.ceil() function to round up a number in Python

NumPy stands for Numerical Python and is a library consisting of multidimensional array objects and a collection of routines for processing those arrays. Using NumPy, mathematical and logical operations on arrays can be performed. It has many built-in methods that are used to perform various mathematical operations and one of them is ceil method, which is used to find the ceiling of a number. In this section, we will learn about numpy.ceil() method.

 

Syntax of numpy.ceil() to round up number in Python

Before using the numpy.ceil() method. First, we have to install the Python NumPy module on our system. We can use the pip command to install the NumPy library as shown below:

pip install numpy

Once we have installed the NumPy module, then we can get access to its various methods by importing the module. The following is the simple syntax of the numpy.ceil() method.

import numpy
numpy.ceil(number)

The ceil method takes one argument ( floating or integer value) and returns the ceiling or the nearest larger or equal integer value.

 

Example of numpy.ceil() method to find the round up of a number

Now let us take examples and see how we can use the numpy.ceil() method to find the round up of a number. See the Python program below which uses the numpy.ceil() method to find the round up of a number.

# importing the numpy module
import numpy as np
# defining numbers
num1 = 3.1
num2 = 3.8
num3 = 4
# Python round up using ceil method
print("Round up value of num1 is :", np.ceil(num1))
print("Round up value of num2 is :", np.ceil(num2))
print("Round up value of num1 is :", np.ceil(num3))

Output:

Round up value of num1 is : 4.0
Round up value of num2 is : 4.0
Round up value of num1 is : 4.0

Notice that the NumPy ceil method returns the ceiling of number but not an integer value. It returns a floating number that contains zero after the decimal point. Let us now confirm the type of return value of the NumPy ceil method by printing the type. See the example below:

# importing the numpy module
import numpy as np
# defining number
num1 = 3.43
# Python round up using numpy ceil method
num = np.ceil(num1)
# printing the type 
print("The type of returned value is :", type(num))

Output:

The type of returned value is : <class 'numpy.float64'>

Notice that the type of return value is NumPy float, not an integer.

 

Method-4: Using simple arithmetic to round up a number in Python

A number can also be rounded up by using simple arithmetic in Python. It casts the first expression into an integer data type and adds 0 or 1 value based on the result of another expression. The other expression finds the modulus of the number with the same denominator and checks if it is greater than 0 or not. If the remainder is greater than 0, it adds one to the first expression, and if it is false, it adds 0 to the first expression. The advantage of this method is that we don't need to import any of the external libraries.

 

Examples of simple arithmetic to round up a number in Python

Now let us take an example and see how we can find the round-up of a number using the simple arithmetic rules. See the example below:

# defining numbers
num1 = 10.44
num2 = 10.6
num3 = 10
div = 5
# Python round up using simple arithmetic method 
print("num1/div: ",int(num1/div) + (num1% div>0))
print("num2/div: ",int(num2/div) + (num2% div>0))
print("num3/div: ",int(num3/div) + (num3% div>0))

Output:

num1/div: 3
num2/div: 3
num3/div: 2

Notice that this method returns the rounded-up value of the result.

 

Method-5: Using floor division operator to round up a number in Python

The symbol for the floor division operator is //. It works in the same way as a simple division operator, /, but it also rounds the number down. So, It is usually used to round down the number in Python. However, we can modify its use to round up a number as well. We can do so by negating the answer by dividing negative numbers. The disadvantage of this method is that it can only be applied to integers.

 

Example of floor division operator to round up a number in Python

Now let us take examples and see how we can use the floor division operator to round up a number in Python. See the Python program below:

# defining numbers
num1 = 10.44
num2 = 10.6
num3 = 10
div = 5
# Python round up using floor division
print("num1/div: ",(-(-num1//div)))
print("num2/div: ",(-(-num2//div)))
print("num3/div: ",(-(-num3//div)))

Output:

num1/div: 3.0
num2/div: 3.0
num3/div: 2

Notice that the floor division method can also be used to round up a number as shown in the above example. We get floating values for the first two and integer values for the last one because floor division of float with integer returns a float value and floor division of integer with an integer returns an integer value.

 

Summary

Rounding means making a number simpler but keeping its value close to what it was. If we round the number to a value slightly larger than the original number is known as rounding up. In this tutorial, we learned about Python round up methods. We discussed four different methods to round up a number in python by taking various examples. We have learned about math.ceil() method, floor division method, simple arithmetic method, and nampy.ceil() method to round up a number in Python. To summarize this tutorial contains all the necessary and popular methods that are commonly used in Python to round up a number.

 

Further Reading

Python math module
Python numpy module
Python rounding number

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment