How to Use the Python round() Function
In this tutorial, you'll learn how to use the Python round() function. The round() function returns a number rounded to a specified number of digits.
Python round() Syntax
The syntax of round() is as follows:
round(number[, digits])
Parameters
- Number: Required. The number that you want to round.
- Digits: Optional. If it is omitted or is None, round() returns the number rounded to the nearest integer.
Examples
Round a Number to 2 Decimal Places
Example:
number = 100.4570
rounded_number = round(number, 2)
print("The rounded number is", rounded_number)
Output:
The rounded number is 100.46
Round a Number to 3 Decimal Places
Example:
number = 100.4570
rounded_number = round(number, 3)
print("The rounded number is", rounded_number)
Output:
The rounded number is 100.457
Round a Number to the Nearest Integer
Example 1:
number = 220.7570
rounded_number = round(number)
print("The rounded number is", rounded_number)
Output:
The rounded number is 221
Example 2:
number = 220.4970
rounded_number = round(number, None)
print("The rounded number is", rounded_number)
Output :
The rounded number is 220
In this tutorial, you've learned how to use the round() function. The Python round() is a function to round and return a number rounded to a specified number of digits.