Converting Between Data Types
int() converts values to integers by truncating (not rounding) the decimal portion
A Decimal Does Not Get Rounded
Suppose a program receives the value 7.9 but needs an integer. The int() function does not choose the nearest integer. Instead, it removes the decimal portion completely. That means 7.9 becomes 7, even though 8 is the nearer rounded value. This single rule explains many results that surprise beginners.
What do you think happens?
What integer does int() produce for 3.99999?
Reveal answer
Answer: 3
int() truncates by removing the decimal part. It does not round according to the size of the decimal portion.
Following the Decimal Portion
Truncation means chopping off the decimal part rather than changing the whole-number part according to a rounding rule. The decimal portion is removed regardless of how large it is. Therefore, 7.1 and 7.9 both become 7, and 3.99999 also becomes 3. For a negative value such as -7.9, removing the decimal portion leaves -7.
Tracing Three Decimal Values
Determine the result of applying int() to 7.9, 7.1, and -7.9.
Process 7.9: Remove the decimal portion .9. The result is 7.
Process 7.1: Remove the decimal portion .1. The result is 7.
Process -7.9: Remove the decimal portion .9 while keeping the whole-number part -7. The result is -7.
The results are 7, 7, and -7. None of these conversions rounds to a nearby integer.
Accepted and Rejected Inputs
The int() function works with floating-point values and with strings that represent whole numbers. It raises an error for strings that contain a decimal portion, such as a decimal string, and it also raises an error for invalid input. The important distinction is between a numeric value that has a decimal portion and a string whose characters contain a decimal point: int() can handle the former, but a decimal string must use an additional conversion step.
| Input category | int() behavior |
|---|---|
| Float value | Converts it to an integer by truncating the decimal portion |
| Whole-number string | Converts it to an integer |
| Decimal string | Raises an error |
| Invalid input | Raises an error |
The input's form matters: a float and a decimal string do not follow the same direct int() path.
The Two-Step Decimal String Path
A decimal string such as '3.14' cannot be passed directly to int(). The source-prescribed solution is to convert the string to a floating-point value first and then convert that result to an integer: int(float('3.14')). The first conversion makes the decimal value available to the second conversion; int() then removes the decimal portion and produces 3.
3Comparing Conversion Roles
int(), float(), and str() should not be treated as interchangeable names. In the supplied material, int() is the operation that produces an integer, float() is used as the intermediate step that turns the decimal string '3.14' into a value int() can process, and str() is identified as a separate conversion function. The source pack does not provide a detailed rule for the exact result of str(), so the reliable rule for this lesson is to focus on int()'s integer result and float()'s role in the two-step decimal-string conversion.
Mistakes to Avoid
Assuming int() rounds to the nearest integer
int() removes the decimal portion instead of rounding it.
Fix:
Apply truncation: 3.99999 becomes 3.Expecting a decimal string to work directly
The source states that int() raises an error for decimal strings.
Fix:
Convert the string to a float first, then pass that result to int().Treating a float and a decimal string as the same input
One is a floating-point value and the other is a string containing decimal characters.
Fix:
Use int() directly for the float, but use int(float('3.14')) for the decimal string.Ignoring invalid input
Invalid input causes int() to raise an error.
Fix:
Check whether the input is a supported float or whole-number string before relying on conversion.
Check Your Prediction
For each input, predict whether int() produces an integer or raises an error. If it produces an integer, state the result: 7.1, 3.99999, '7', and '3.14'. Then explain why the last input requires a different path.
Hints
- For a floating-point value, remove the decimal portion rather than rounding.
- A whole-number string is supported by int().
- A decimal string must first be converted with float().
- Use int() when the desired result is an integer. Remember that it truncates by removing the decimal portion, so 7.9 becomes 7 and 3.99999 becomes 3. It works with floats and whole-number strings, but decimal strings and invalid input raise an error. To process a decimal string such as '3.14', convert it to a float first and then apply int().
Key Takeaways
- int() converts values to integers by truncating rather than rounding.
- The decimal portion is removed completely, regardless of its size.
- Floats and whole-number strings can be converted directly, while decimal strings and invalid input raise an error.
- A decimal string can be converted with int(float('3.14')).
- float() can serve as the intermediate step for a decimal string, while str() is a separate conversion function.