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

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

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?

ベストアンサー1

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下向きに丸められ、math.floor-Infinity に向かって丸められるため、値が小さくなります (正の値が小さくなるか、負の値が大きくなります)。

Python の整数は任意の精度なので、非常に大きな浮動小数点数でも整数として表現できます。(他の言語では、この慣用句は整数型の最大値よりも大きい浮動小数点数では失敗する可能性があります。)

おすすめ記事