The round()
function in Python is used for rounding off floating-point numbers to a desired level.
The round()
function accepts two arguments:
- number: This is the floating-point number you want to round.
- ndigits (optional): This is an integer that specifies the number of decimal places to retain in the rounded result. The default value is 0, which rounds the number to the nearest integer.
This article is about the round() function. It explains how to use it, what it can do, and gives examples to help you understand better.
Here’s the basic syntax:
rounded_number = round(number, ndigits)
How To Use The Round Function In Python With Examples
Here are various ways to utilize the round()
function with illustrative examples:
1. Rounding To The Nearest Integer
number = 3.14159
rounded_number = round(number)
print(rounded_number) # Output: 3
number = -2.71828
rounded_number = round(number)
print(rounded_number) # Output: -3
As you can see, the default behavior rounds the number to the nearest whole number. Positive numbers are rounded up if the decimal part is greater than or equal to 0.5, and negative numbers are rounded down in similar conditions.
2. Rounding To A Specific Number Of Decimal
The ndigits argument allows you to control the precision of the rounded result. Positive values of ndigits specify the number of decimal places to retain, while negative values round towards the leftmost digits (integer part).
number = 3.14159
# Round to two decimal places
rounded_number = round(number, 2)
print(rounded_number) # Output: 3.14
number = 123.45678
# Round to three decimal places
rounded_number = round(number, 3)
print(rounded_number) # Output: 123.457
# Round to thousands place (negative ndigits)
number = 12345.6789
rounded_number = round(number, -3)
print(rounded_number) # Output: 12000
3. Rounding Towards Specific Directions
While round()
offers basic rounding, Python’s math
module provides functions for more specific rounding behaviors:
math.ceil(number)
: This function rounds the number up to the nearest integer, regardless of the decimal part.math.floor(number)
:Â This function rounds the number down to the nearest integer.
import math
number = 2.5
# Round up using ceil
rounded_number = math.ceil(number)
print(rounded_number) # Output: 3
number = -1.5
# Round down using floor
rounded_number = math.floor(number)
print(rounded_number) # Output: -2
4. Handling Edge Cases
Rounding exactly at the midpoint (0.5): When the number falls precisely at the midpoint between two integers (e.g., 2.5 or -1.5), the round()
 function rounds towards even by default. This behavior can be modified using the roundhalf
 module (pip install roundhalf
).
from roundhalf import round_half_even
number = 2.5
rounded_number = round_half_even(number)
print(rounded_number) # Output: 3
number = -1.5
rounded_number = round_half_even(number)
print(rounded_number) # Output: -2
Rounding strings containing numbers: Attempting to directly round strings using round()
 will result in a TypeError
. You need to convert the string to a float first:
number_string = "3.14159"
# Convert string to float before rounding
number = float(number_string)
rounded_number = round(number, 2)
print(rounded_number) # Output: 3.14
5. Formatting Rounded Numbers
The round()
function returns a float. You can use Python’s built-in formatting functionalities to display the rounded number with a desired number of decimal places:
- f-strings:
number = 3.14159
rounded_number = round(number, 2)
print(f"Rounded to two decimals: {rounded_number:.2f}") # Output: Rounded to two decimals: 3.14
format()
 method:
number = 123.45678
rounded_number = round(number, 3)
print("Rounded to three decimals: {:.3f}".format(rounded_number)) # Output: Rounded to three decimals: 123.457
These methods allow you to control the precision of the displayed decimal places, making the output more readable and consistent with your formatting requirements.