Round a floating-point number down to the nearest integer?

floating-point, integer, python, rounding

Solution

int(x)

Conversion to integer will truncate (towards 0.0), like `math.trunc`. For non-negative numbers, this is downward.

If your number can be negative, this will round the magnitude downward, unlike `math.floor` which rounds towards -Infinity, making a lower value. (Less positive or more negative).

Python integers are arbitrary precision, so even very large floats can be represented as integers. (Unlike in other languages where this idiom could fail for floats larger than the largest value for an integer type.)

Problem

I want to take a floating-point number and round it down to the nearest integer. However, if it's not a whole, I always want to round down the variable, regardless of how close it is to the next integer up. Is there a way to do this?

Original source