round()
Published Jun 14, 2021Updated Sep 3, 2021
Contribute to Docs
Takes a number and an integer as parameters, and returns the number with decimal places equal to the integer.
Syntax
round(number, digits)
number
is the number to be rounded. (Required)digits
is the number of decimals when rounding the number. Default is 0.
Example 1
The default number of decimal places used is 0.
my_number = 3.14159265359my_rounded_number = round(my_number)print(my_rounded_number)# Output: 3
Example 2
round()
can be used with any number of decimal places. Numbers >=
5 will round up.
my_number = 3.14159265359my_rounded_number = round(my_number, 4)print(my_rounded_number)# Output: 3.1416
Example 3
round()
can be useful when comparing numbers, for example, when comparing fractions and decimals.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Skill path
Analyze Data with Python
Learn to analyze and visualize data using Python and statistics.Includes 8 CoursesWith CertificateIntermediate13 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours