Syntax Atlas

Python round(), floor(), ceil() and int() With Negative Numbers, Measured

What Python’s round, math.floor, math.ceil, int and math.trunc return for halves and negative numbers, next to JavaScript’s Math.round, Math.floor, Math.ceil and Math.trunc — measured on our own computer.

What we measured: Linux x86_64 (kernel 7.0.0-34-generic), CPython 3.12.13 and CPython 3.14.4, Node.js v20.20.2, on the 11 inputs below. Not measured: Windows and macOS, browser JavaScript engines, PyPy, other Python and Node.js versions, other languages, and round with a number of digits.

What the measurement shows

Measured results

Each value is what the interpreter printed: Python shows repr() of the result, JavaScript shows the result with negative zero written as -0. Both CPython versions returned the same value in every cell, so the Python columns are shown once.

Rounding: round(x) and Math.round(x)

xPython round(x)JavaScript Math.round(x)
2.523
-2.5-2-2
3.544
-3.5-4-3
0.501
-0.50-0
2.733
-2.7-3-3
-0.40-0
777
-7-7-7

Python: math.floor, math.ceil, int, math.trunc and x // 1

xmath.floor(x)math.ceil(x)int(x)math.trunc(x)x // 1
2.523222.0
-2.5-3-2-2-2-3.0
3.534333.0
-3.5-4-3-3-3-4.0
0.501000.0
-0.5-1000-1.0
2.723222.0
-2.7-3-2-2-2-3.0
-0.4-1000-1.0
777777
-7-7-7-7-7-7

JavaScript: Math.floor, Math.ceil and Math.trunc

xMath.floor(x)Math.ceil(x)Math.trunc(x)
2.5232
-2.5-3-2-2
3.5343
-3.5-4-3-3
0.5010
-0.5-1-0-0
2.7232
-2.7-3-2-2
-0.4-1-0-0
7777
-7-7-7-7

Further reading

The rules are defined in the Python documentation for round, the math module documentation and the ECMAScript specification for Math.round; we link to them and do not quote them.