Built-in Functions
round()
Round a number to a chosen number of digits.
round(number, ndigits=None)
round() returns number rounded to ndigits precision after the decimal point.
Python uses half-to-even rounding (banker's rounding) to minimize statistical bias on exact halves (e.g. round(2.5) == 2 and round(3.5) == 4).
Parameters
| Name | Description |
|---|---|
| number | The number to round. |
| ndigits | Optional number of decimal places to keep. |
Returns
A rounded number.
Try it
Run it and change it
price = 3.14159 print(round(price, 2))
Worth knowing
- Banker's rounding: Exact halves round toward the nearest even integer rather than always rounding up.