Working with Floating-Point Numbers
int() converts values to integers by truncating (not rounding) the decimal portion
The Decimal That Disappears
When a value contains a decimal portion, int() does not ask whether the value is closer to the next integer. Instead, it removes the decimal portion completely. That behavior is called truncation. For example, 3.99999 becomes 3, not 4.
What do you think happens?
What integer does int() produce from 3.99999?
Reveal answer
Answer: 3
int() truncates the decimal portion. It does not round the value upward, even when the decimal portion is very close to 1.
Tracing Truncation
The important operation is removal, not comparison. int() keeps the integer portion and discards the decimal portion. The size of the discarded portion does not change this rule: a value such as 3.99999 still becomes 3. For a negative value, removing the decimal portion gives the integer portion closer to zero, so -3.99999 becomes -3.
Inputs That Pass or Fail
int() works with floating-point values and with strings that contain whole numbers. It raises an error for strings containing decimal values and for invalid input. This means the visible presence of digits is not enough: the input must also have a form that int() can convert directly.
| Input category | int() result |
|---|---|
| Floating-point value | Converts to an integer by truncating the decimal portion |
| Whole-number string | Converts to an integer |
| Decimal string | Raises an error |
| Invalid input | Raises an error |
The source form of the input determines whether int() can convert it directly.
Choosing the Conversion Path
Converting a Decimal String
A value is stored as the string '3.14'. How can it be converted to an integer?
Recognize the input form: The value is a decimal string, so int() cannot convert it directly.
Convert the string to a floating-point value: Use float('3.14') as the first conversion step.
Convert the floating-point value to an integer: Pass the result to int(). The decimal portion is truncated.
int(float('3.14')) produces the integer 3.
| Function | Role in this topic |
|---|---|
| int() | Converts a supported value to an integer; decimal portions are truncated |
| float() | Provides the intermediate conversion needed when a decimal string must be converted before int() |
| str() | Is a different conversion function and is not the direct operation described for producing an integer |
Mistakes to Avoid
Assuming int() rounds to the nearest integer.
int() truncates by removing the decimal portion, regardless of how large that portion is.
Fix:
Expect 3.99999 to become 3.Passing a decimal string directly to int().
int() works with whole-number strings, but a decimal string causes an error.
Fix:
Convert the decimal string to a floating-point value first, then convert that result to an integer.Treating int(), float(), and str() as interchangeable.
The functions have different roles in the conversion process described here.
Fix:
Use int() when the target is an integer, and use the float-then-int path for a decimal string.
Check Your Understanding
For each input, decide whether int() can convert it directly. If it can, state the resulting integer. If it cannot, identify the conversion path that should be used instead: 8.75, '8', '8.75', and 'not a number'.
Hints
- A floating-point value can be converted by truncating its decimal portion.
- A whole-number string is different from a decimal string.
- A decimal string requires float() before int().
- Invalid input raises an error.
- int() converts supported values to integers.
- When given a floating-point value, int() truncates by removing the decimal portion rather than rounding.
- Floating-point values and whole-number strings can be converted directly, while decimal strings and invalid input cause an error.
- A decimal string can be converted by using float() first and int() second, as in int(float('3.14')).
- int(), float(), and str() have different roles; they are not interchangeable conversion operations.
Key Takeaways
- int() removes the decimal portion instead of rounding.
- A value such as 3.99999 becomes 3.
- int() accepts floating-point values and whole-number strings, but decimal strings and invalid input cause an error.
- Use int(float('3.14')) when a decimal string must become an integer.
- Choose int(), float(), or str() according to the conversion role required.