The round() Function and Rounding Behavior
The result is not 4 because int() does not round.
Try it: Type conversion: int(), float(), str(), bool()
Exactly how int(), int(text, base), float(), str() and bool() turn a value into another type: Python's real parsing rules for text (whitespace, sign, underscores, 0x/0o/0b prefixes, inf/nan, exponents) and its truthiness rules.
How it works
- int(text) strips surrounding whitespace, allows one sign, then reads digits (underscores only between digits); any other character raises ValueError: invalid literal for int().
- int(text, base) reads digits in that base; base 0 takes the base from a 0x / 0o / 0b prefix like a Python literal.
- float(text) accepts decimals like '.5', '5.', '1e3' and the names inf, infinity and nan, then rounds to the nearest double.
- int() of a float truncates toward zero; str() gives the printed form (repr digits for floats).
- bool() is truthiness: '', 0, 0.0 and None are False; every non-empty string — even '0' or 'False' — is True.
Default run (8 steps): int(' -0x_1F ', 0): the argument is a str of 10 characters. … int(' -0x_1F ', 0) → -31
Simplified: The argument is a Python literal typed as text (ASCII only; escapes \t \n \r). Unicode digits and bytes arguments are not modelled.
Loading the simulation…
The Conversion Trap
A decimal value can look close to the next whole number, but int() does not ask which whole number is nearest. It converts the value to an integer by removing its decimal portion. That is truncation, not rounding. This distinction is the central idea behind the rounding behavior covered in this article.
What do you think happens?
What integer does int() produce from the value 3.99999?
Reveal answer
3
int() truncates the decimal portion. It does not round 3.99999 up to 4.
Removing the Decimal Portion
Truncation means removing the decimal part entirely. The size of the decimal portion does not change this rule: even when the decimal portion is very close to the next integer, int() still removes it rather than rounding the value. For example, 3.99999 becomes 3.
The correct mental model is chop off the decimal portion, not choose the nearest integer.
Accepted and Rejected Inputs
int() works with floats and with strings that contain whole numbers. It raises an error for strings that contain decimal values and for invalid input. This means the visible type and format of the input matter, not just the numerical idea a person may have intended.
| Input form | int() behavior |
|---|---|
| A float | Can convert; the decimal portion is truncated |
| A whole-number string | Can convert |
| A decimal string | Raises an error |
| Invalid input | Raises an error |
The source pack identifies these input categories for int().
Choosing the Conversion Function
int(), float(), and str() represent different conversion intentions. int() produces an integer and truncates a float's decimal portion. The source pack specifically describes a useful two-step path for a decimal string: first convert the string to a float, then convert that float to an integer. The expression int(float('3.14')) follows that path.
3A Complete Conversion Trace
Converting a decimal string
Determine the result of int(float('3.14')).
- 1Read the original input
'3.14' is a string containing a decimal value.
- 2Convert the string to a float
float('3.14')produces the decimal value 3.14. - 3Convert the float to an integer
int()truncates the decimal portion rather than rounding it.
The final integer result is 3.
This trace separates two issues that are easy to confuse. The first conversion handles the string format. The second conversion handles the numeric value and removes its decimal portion. The result is not 4 because int() does not round.
Mistakes Beginners Make
Common mistake Assuming int() rounds to the nearest integer.
What it looks likeTreating int(3.99999) as if it produced 4.Why it failsint()truncates the decimal portion, even when that portion is very large.Do this insteadExpect the decimal portion to be removed, so 3.99999 becomes 3.
Common mistake Passing a decimal string directly to int().
What it looks likeUsing int('3.14').Why it failsThe source identifies decimal strings as inputs that raise an error.
Do this insteadConvert the string to a float first, then pass the float to int(): int(float('3.14')).
Common mistake Treating int(), float(), and str() as interchangeable.
What it looks likeChoosing int() when the required result is still a decimal value or a string representation.
Why it failsThe functions represent different conversion paths, and
int()changes a decimal value into an integer by truncation.Do this insteadChoose the function according to the intended result: integer conversion, float conversion, or string conversion.
Practice the Rule
For each input, decide whether int() can convert it. If it can, state whether the decimal portion is preserved or removed. If it cannot, identify why the input form is a problem: 7.8, '7', '7.8', or invalid input.
Hints
- A float can be converted by
int(), but its decimal portion is truncated. - A whole-number string can be converted.
- A decimal string raises an error.
- Invalid input raises an error.
- 1Use
int()when the intended result is an integer and truncation is acceptable. Do not treat it as a rounding operation. When the starting value is a decimal string, use the source-supported sequence of converting it to a float first and then to an integer.
Key Takeaways
int()converts values to integers by truncating their decimal portions.- Truncation removes the decimal portion entirely; it does not round to the nearest integer.
int()works with floats and whole-number strings, but decimal strings and invalid input raise an error.- To convert a decimal string such as '3.14', first convert it to a float and then pass it to
int(). int(),float(), andstr()represent different conversion intentions and should not be treated as interchangeable.