round() Builtin Function

Python round()

Python round() function is used to round the given number to specified number of decimal digits.

Syntax

The syntax of round() function is

round(number, ndigits)

where

numberThe number whose decimal places has to be rounded.
ndigits[Optional] Number of decimal places after the decimal point to be preserved in the output. Default value is 0.

round() function returns an int if ndigits is not specified, or float otherwise.

Examples

Round a Number to Nearest Integer

In this example, we take a float value with three decimal places, and find its rounded value.

Python Program

number = 3.141
result = round(number)
print('Original Value :', number)
print('Rounded Value :', result)
Run

Output

Original Value : 3.141
Rounded Value : 3

Round Number to Specific Decimal Places

In this example, we take a float value with three decimal places, and round it to two decimal places.

Python Program

number = 3.141
ndigits = 2
result = round(number, ndigits)
print('Original Value :', number)
print('Rounded Value :', result)
Run

Output

Original Value : 3.141
Rounded Value : 3.14

Summary

In this tutorial of Python Examples, we learned the syntax of round() builtin function, and how to use it to round the given number to a specified number of decimal places, with the help of examples.