Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

round()

Rounds a number to the requested decimal position, or returns the nearest integer when the position is omitted.

Version: Python 3.14; repository image, production runtime not attested

Syntax

round(number, ndigits=None)

Rounds a number to the requested decimal position, or returns the nearest integer when the position is omitted.

For exact halfway cases built-in numbers choose the even result, including negative halfway values.

With no decimal position a built-in float rounds to the nearest integer. Exact halfway values select the even neighbor, giving 2, 4 and −2. A negative ndigits rounds left of the decimal point, hence 151 becomes 150. Decimal halfway text need not be exactly halfway when stored in binary.

assert tuple(round(x) for x in (2.5, 3.5, -2.5)) == (2, 4, -2)
assert round(151, -1) == 150 and isinstance(round(2.5, 0), float)

Examples

print(round(2.5), round(3.5), round(-2.5))
print(round(151, -1))

Result

2 4 -2
150

With no decimal position a built-in float rounds to the nearest integer. Exact halfway values select the even neighbor, giving 2, 4 and −2. A negative ndigits rounds left of the decimal point, hence 151 becomes 150. Decimal halfway text need not be exactly halfway when stored in binary.

Sources