Format calculated precision
Presenting measurements or computed values with a fixed number of decimal places.
Python · Function
round() rounds a number to a selected decimal precision, or to the nearest integer when ndigits is omitted. Ties for built-in numeric types use round-half-to-even.
round() returns a number rounded to a requested decimal precision. Without ndigits, or when it is None, it returns the nearest integer; otherwise the result normally retains the type of number, and custom objects can define the operation through __round__().
round(number, ndigits=None)| Name | Type | Description |
|---|---|---|
number | number | Number to round; the documentation names this argument number. |
ndigits | int | None | Optional integer precision after the decimal point; positive, zero, and negative integers are valid, and None has the same effect as omission. |
The nearest integer when ndigits is omitted or None; otherwise the rounded value, generally with the same type as number.
print(round(2.675, 2))
print(round(1.5))
print(round(25, -1))Demonstrates decimal-place rounding, ties-to-even behavior, and negative precision.
Presenting measurements or computed values with a fixed number of decimal places.
Reducing a value to tens, hundreds, or other powers of ten with negative ndigits.
Supporting domain-specific numeric classes that need customized rounding.
round(number, ndigits=None) rounds number to the requested decimal precision. If ndigits is omitted or None, the result is the nearest integer.
Follow-up: What happens for a tie? The built-in types choose the nearest multiple, and an equally close tie is resolved toward the even choice.
ndigits may be any integer, including positive, zero, or negative values. Negative values round to coarser powers of ten, such as round(25, -1) producing 20.
Follow-up: Does ndigits have to be nonnegative? No; the documentation explicitly permits positive, zero, and negative integers.
When ndigits is omitted or None, round() returns an integer. Otherwise, its return type is the same as number for the built-in types that support it, and general objects can provide __round__().
Follow-up: How can a custom numeric type control rounding? It implements __round__, which round() delegates to for a general object.
__round__intfloat