How to Get Integer Part of a Number in Python
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This code demonstrates two methods to extract the integer part from a floating-point number in Python.
The number 12.34567 is used. You can use the built-in int() function or the math.floor() function from the math module.
- Method 1:
int(number)truncates the decimal part towards zero. - Method 2:
math.floor(number)rounds down to the nearest integer.
Key difference: For positive numbers, both produce the same result. For negative numbers, int() truncates towards zero (e.g., int(-1.2) → -1), while math.floor() rounds down (e.g., math.floor(-1.2) → -2).