In this tutorial, we will see the Round() function of Python and some of its use.
What is Rounding in Mathematics?
Rounding means making a number simpler and shorter but keeping its value close to what it was. Of course, the result is less accurate but easier to use. For example, you got 93.89% in exams, but to make it simpler and shorter you round it and make it 94% which is close to the original percentage.
How to Round numbers?
- Decide which is the last digit to keep
- Leave it the same if the next digit is less than 5 (this is called rounding down)
- But increase it by 1 if the next digit is 5 or more (this is called rounding up)
Examples:
45.3456 = 45.35 (rounding upto 2 decimal places)
34.32437 = 34.324 (rounding upto 3 decimal places)
Rounding in Python | Python round() function
Python has the round()
function that returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. It takes two parameters, the first is the number to be rounded and the second is the number of decimals to use when rounding the number. The second is optional and by default, it is 0.
Syntax round(number, digits)
x = round(5.76543, 2)
print(x)
x = round(5.76543)
print(x)
Related:
Python Basics
List, Set, Tuple, and Dictionary in Python
Python Reverse List items
Python Round() Function
Python Multiline comment
Power in Python | Python pow()
Python range() Function
Square Root in Python
Python for i in range