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.)
Answer from Ghostwriter on Stack Overflowpython - Round a floating-point number down to the nearest integer? - Stack Overflow
How to round numbers below?
How to round up without math modules?
how to limit or round a float to only two decimals without rounding up
Videos
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.)
One of these should work:
import math
math.trunc(1.5)
> 1
math.trunc(-1.5)
> -1
math.floor(1.5)
> 1
math.floor(-1.5)
> -2
I wrote a code in which a variable is a number, but Im asked to always round it below. How do I do this, since "round" will round it to the nearest?