round() function in Python (original) (raw)

round() is a built-in Python function used to round numbers to a specified number of decimal places.

**Example: This example rounds a decimal number to the nearest integer.

Python `

n = 45.7 print(round(n))

`

**Explanation: round(n) rounds 45.7 to the nearest integer, which is 46.

Syntax

round(number, ndigits)

**Parameters:

**Return: Returns a rounded int or float.

round() Without ndigits

When the second parameter (ndigits) is not provided, round() automatically rounds the number to the nearest integer. If the number is already an integer, it remains unchanged.

For decimal numbers, Python rounds to the closest integer using its standard rounding rule.

Python `

print(round(15)) print(round(51.6)) print(round(51.5)) print(round(51.4))

`

**Explanation:

round() With ndigits

When the second parameter (ndigits) is provided, round() rounds the number to the specified number of decimal places.

It checks the digit after the required decimal place and rounds accordingly using Python’s standard rounding rule.

Python `

print(round(2.665, 2)) print(round(2.676, 2)) print(round(2.673, 2))

`

**Explanation:

round() with Negative Numbers

round() works the same way for negative numbers as it does for positive numbers. It rounds to the nearest integer (or specified decimal place) based on closeness. When the value ends in .5, Python applies the “round to nearest even” rule.

Python `

print(round(-3.2)) print(round(-4.7)) print(round(-2.5)) print(round(-2.675, 2)) print(round(-1234, -2))

`

Output

-3 -5 -2 -2.67 -1200

**Explanation

Rounding with math Module

The math module provides functions to always round a number down or up, regardless of its decimal value.

import math

n = 3.6 print(math.floor(n)) print(math.ceil(n))

`

**Explanation:

Rounding with NumPy

The NumPy library provides the np.round() function to round multiple numeric values at once. It is useful when working with arrays, especially in data analysis and scientific computing.

Python `

import numpy as np a = np.array([-2.675, -1.23456789, -3.14159265]) print(np.round(a, 3))

`

Output

[-2.675 -1.235 -3.142]

**Explanation: np.round(a, 3) rounds each element in array a to 3 decimal places.

Rounding Up

When rounding a number to the nearest integer using round(), values with decimal part .5 or greater are rounded upward (based on standard rounding rules).

Python `

print(round(12)) print(round(12.7))

`

**Explanation:

Rounding Down

Numbers with a decimal part less than .5 are rounded down to the nearest integer using round().

Python `

print(round(12)) print(round(12.1)) print(round(12.4)) print(round(12.5))

`

**Explanation:

Errors in round()

round() works only with numeric values (int, float). If a non-numeric value is passed, Python raises a TypeError.

Python `

print(round("a", 2))

`

**Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 1, in
TypeError: type str doesn't define __round__ method

**Explanation: round("a", 2) raises an error because strings do not support the rounding operation.

Practical Applications

Rounding is commonly used to limit decimal places in calculations, especially when working with fractions that produce long decimal results.

Python `

b = 1/3 print(b) print(round(b, 2))

`

Output

0.3333333333333333 0.33

**Explanation: round(b, 2) limits the value of b to two decimal places, making the output cleaner and easier to use in reports or calculations.