round() Builtin Function
Python round()
Python round() function is used to round the given number to specified number of decimal digits.
In this tutorial, you will learn the syntax of round() function, and then its usage with the help of example programs.
Syntax
The syntax of round() function is
round(number, ndigits)
where
Parameter | Description |
---|---|
number | The 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
1. 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)
Output
Original Value : 3.141
Rounded Value : 3
2. 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)
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.