How To Round Python Numbers

Python numbers are very easy to round. In this blog post, we’ll go over a few simple methods to round numbers in Python.

Checkout this video:

Rounding Numbers in Python

It is often useful to round a floating point number to the nearest whole number, or to some other nearby value. This can be done with the round() function, which takes two arguments. The first is the number to be rounded, and the second is the number of decimal places to round to (or how many digits after the decimal point to keep).

If we want to round 3.14159 to two decimal places, we could do it like this:

“`python
rounded = round(3.14159, 2)
print(rounded) # Prints 3.14
“`

If we want to round 3.14159 up to the next highest whole number, we could do it like this:
“`python
rounded = round(3.14159, 0)
print(rounded) # Prints 4.0
“`

The round() Function

Python’s round() function returns a floating-point number rounded to the specified number of decimal places. The decimal module provides support for fast correctly-rounded decimal floating-point arithmetic.

When called with two arguments, round() returns a float rounded to the specified number of decimals. When called with one argument, or with an omitted second argument, it returns a float rounded to 0 decimal places (nearest whole number).

The decimal Module

Python has a built-in module called decimal that helps perform calculations with floating point numbers. It is useful to know how these numbers are represented in binary so you can understand what is going on under the hood.

The decimal module provides functions for arithmetic, comparisons, and rounding of Decimal numbers. It can also be used to perform other operations such as converting between binary and octal representation.

To use the decimal module, you need to import it first:
“`python
import decimal
“`
Now let’s say we have a number like 3.14 and we want to round it to 2 decimal places. We can do this with the following code:
“`python
rounded = round(decimal.Decimal(‘3.14’), 2)
print(rounded) # prints 3.14
“`

The math Module

Python has a built-in module called math that contains a number of useful mathematical functions. One of these functions is the round() function, which rounds a number to a specified number of decimal places.

The round() function takes two arguments: the number to be rounded and the number of decimal places to round it to. For example, if we want to round the number 4.7 to two decimal places, we would use the following code:

round(4.7, 2)

This would return the value 4.70.

The cmath Module

Python’s cmath module allows you to work with complex numbers, and perform complex mathematical operations. Complex numbers are imaginary numbers, i.e. they have a real and an imaginary part. The imaginary part is denoted by the letter “i”, and is the square root of -1.

The cmath module defines the following functions:

-abs(x)
Returns the absolute value of x
-acos(x)
Returns the arc cosine of x, in radians
-acosh(x)
Returns the inverse hyperbolic cosine of x

How to Round a Number to the Nearest Integer

When working with numbers in Python, you will sometimes need to round a number to the nearest integer. The built-in round() function allows you to do this easily.

To round a number to the nearest integer, you can use the round() function. The round() function takes a single argument, the number you want to round. It will then return the closest integer to that number.

For example, if you have a float value that you want to round to the nearest integer, you can do so like this:
“`
>>> num = 4.7
>>> round(num)
5
“`
In this example, we have a float value (4.7) that we want to round to the nearest integer. We do this by passing it as an argument to the round() function. The function then returns the closest integer, in this case 5.

How to Round a Number Up or Down

If you want to round a number up, you use the ceil function. This function rounds a number up to the nearest integer. So, if you have a number like 23.7, it will round it up to 24.

To round a number down, you use the floor function. This takes a number and rounds it down to the nearest integer. So, if you have the number 23.7, it will round it down to 23.

There is also a function called “round” that rounds numbers to the nearest integer. However, this rounds numbers up or down based on whether they are .5 or higher or .5 or lower.

How to Round a Number to a Specific Decimal Place

To round a number to a specific decimal place, you need to use the round() function. The round() function takes two arguments: the number you want to round and the number of decimal places you want to round to. For example, if you want to round the number 8.34542 to 8 decimal places, you would use this code:

number = 8.34542
rounded = round(number, 8)
print(rounded)

The code above would print out 8.345420000.

How to Use the round() Function with Complex Numbers

When you’re working with complex numbers in Python, you may need to use the round() function to round them to a certain number of decimal places. In this article, we’ll show you how to do that.

Python has a built-in round() function that takes two arguments: the number to be rounded and the number of decimal places to round it to. For example, if we have a complex number like 3.14159 and we want to round it to two decimal places, we would use the following code:

complex = 3.14159
rounded = round(complex, 2)
print(rounded) # prints 3.14

As you can see, the complex number is rounded to two decimal places and the result is printed out.

If we want to round a complex number to a different number of decimal places, we just need to change the second argument in the round() function. For example, if we want to round our complex number to three decimal places, we would use this code:

complex = 3.14159
rounded = round(complex, 3)
print(rounded) # prints 3.142

Further Reading

If you want to know more about rounding numbers in Python, check out the official documentation on the round function. You can also learn about other ways to format numbers for output in the Python Formatting section of our course.

Scroll to Top