The round() function in Python is a built-in function used to round a number to a specified number of digits. It returns the number rounded to the nearest integer or to a specified number of decimal places.
The round() function is commonly used in programs where approximate values or formatted numbers are required, such as financial calculations, measurements, and statistical results.
round(number, ndigits)
number The numeric value that needs to be rounded.
ndigits (optional) The number of decimal places to which the number should be rounded.
print(round(4.6))
print(round(4.2))
Output
5
4
Explanation
4.6 is rounded up to 5.4.2 is rounded down to 4.print(round(3.14159, 2))
Output
3.14
Explanation
print(round(7.856, 1))
Output
7.9
Explanation
ndigitsNegative values for ndigits round numbers to tens, hundreds, etc.
print(round(125, -1))
print(round(125, -2))
Output
120
100
Explanation
-1 rounds the number to the nearest ten.-2 rounds the number to the nearest hundred.print(round(2.5))
print(round(3.5))
Output
2
4
Explanation
Python uses "round half to even" (also called banker's rounding):
2.5 → rounded to 2 (nearest even number)3.5 → rounded to 4 (nearest even number)price = 123.4567
rounded_price = round(price, 2)
print("Rounded Price:", rounded_price)
Output
Rounded Price: 123.46
Explanation
round() is a built-in Python function used to round numbers.ndigits parameter controls the number of decimal digits.ndigits round numbers to tens, hundreds, etc..5.