Working with Floating-Point Numbers
int() works with floating-point values and with strings that contain whole numbers.
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
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?
- 1Recognize the input form
The value is a decimal string, so
int()cannot convert it directly. - 2Convert the string to a floating-point value
Use
float('3.14')as the first conversion step. - 3Convert 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 |
int(), float(), and str() should not be treated as interchangeable conversion steps.
Mistakes to Avoid
Common mistake Assuming int() rounds to the nearest integer.
What it looks likeTreating 3.99999 as though it becomes 4.
Why it failsint()truncates by removing the decimal portion, regardless of how large that portion is.Do this insteadExpect 3.99999 to become 3.
Common mistake Passing a decimal string directly to int().
What it looks likeTrying to convert '3.14' directly with int().Why it failsint()works with whole-number strings, but a decimal string causes an error.Do this insteadConvert the decimal string to a floating-point value first, then convert that result to an integer.
Common mistake Treating int(), float(), and str() as interchangeable.
What it looks likeUsing str() as though it were the direct operation for producing an integer.
Why it failsThe functions have different roles in the conversion process described here.
Do this insteadUse 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()beforeint(). - Invalid input raises an error.
- 1
int()converts supported values to integers. - 2When given a floating-point value,
int()truncates by removing the decimal portion rather than rounding. - 3Floating-point values and whole-number strings can be converted directly, while decimal strings and invalid input cause an error.
- 4A decimal string can be converted by using
float()first andint()second, as inint(float('3.14')). - 5
int(),float(), andstr()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(), orstr()according to the conversion role required.