YouTip LogoYouTip

Func Number Round

## Python round() Function The built-in **`round()`** function in Python is used to round a floating-point number to a specified number of decimals. --- ### Description The `round()` function returns a floating-point number rounded to `n` digits after the decimal point. If the number of decimals `n` is omitted, it rounds the input number to the nearest integer. --- ### Syntax The syntax for the `round()` function is as follows: ```python round(number, ndigits) ``` ### Parameters * **`number`**: The numeric expression (integer or float) that you want to round. * **`ndigits`** *(Optional)*: The number of decimal places to round to. * If omitted or `None`, it defaults to `0` and returns the nearest integer. * If positive, it rounds to the specified number of decimal places. * If negative, it rounds to the left of the decimal point (e.g., tens, hundreds, thousands). ### Return Value * Returns an **integer** if `ndigits` is omitted or `None`. * Returns a **floating-point number** if `ndigits` is specified (even if it is `0`). --- ### Basic Examples Here is a basic demonstration of how the `round()` function works in Python: ```python # Rounding to 2 decimal places print("round(80.23456, 2) :", round(80.23456, 2)) # Rounding to 3 decimal places print("round(100.000056, 3) :", round(100.000056, 3)) # Rounding a negative number to 3 decimal places print("round(-100.000056, 3):", round(-100.000056, 3)) ``` **Output:** ```text round(80.23456, 2) : 80.23 round(100.000056, 3) : 100.0 round(-100.000056, 3): -100.0 ``` --- ### Advanced Examples & Edge Cases #### 1. Rounding to the Nearest Integer (Omitting `ndigits`) When you do not provide the second argument, Python rounds to the nearest integer and returns an `int` type. ```python print(round(10.4)) # Output: 10 print(round(10.6)) # Output: 11 print(type(round(10.6))) # Output: ``` #### 2. Negative `ndigits` (Rounding to Tens, Hundreds, etc.) Using a negative integer for `ndigits` allows you to round to the left of the decimal point. ```python # Round to the nearest ten print(round(123.45, -1)) # Output: 120.0 # Round to the nearest hundred print(round(123.45, -2)) # Output: 100.0 ``` --- ### Important Considerations: "Banker's Rounding" In Python 3, the `round()` function implements **"Banker's Rounding"** (round half to even). When a number is exactly halfway between two possible rounded values (e.g., ending in `.5`), Python rounds to the nearest **even** number. #### Example: ```python # Halfway cases print(round(2.5)) # Output: 2 (rounds down to the nearest even number) print(round(3.5)) # Output: 4 (rounds up to the nearest even number) print(round(4.5)) # Output: 4 (rounds down to the nearest even number) print(round(5.5)) # Output: 6 (rounds up to the nearest even number) ``` #### Floating-Point Precision Issues Because computers represent floating-point numbers in binary, some decimal fractions cannot be represented precisely. This can lead to unexpected rounding behaviors: ```python # 2.675 is represented internally as slightly less than 2.675, # so it rounds to 2.67 instead of 2.68. print(round(2.675, 2)) # Output: 2.67 ``` If your application requires exact decimal rounding (such as in financial calculations), you should use Python's built-in `decimal` module instead: ```python from decimal import Decimal, ROUND_HALF_UP # Accurate rounding using the Decimal module num = Decimal('2.675') rounded = num.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) print(rounded) # Output: 2.68 ```
← Func Number SqrtFunc Number Pow β†’