Concepts / The round() Function and Rounding Behavior

The round() Function and Rounding Behavior

int() converts values to integers by truncating (not rounding) the decimal portion

  • Programming

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?

  • 3
  • 4
  • An error because the value has decimals
Reveal answer

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.

int() removes .999993.999993
What happens when int() converts a decimal value instead of rounding it?

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.

3.14'3.14''3'invalid input
Which input forms can int() convert, and which forms cause a conversion error?
Input formint() behavior
A floatCan convert; the decimal portion is truncated
A whole-number stringCan convert
A decimal stringRaises an error
Invalid inputRaises 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.

int() truncatesfloat() conversionstr() representation3.1433.14'3.14'
How do int(), float(), and str() differ when choosing a conversion path?
python
Output
3

A Complete Conversion Trace

Converting a decimal string

Determine the result of int(float('3.14')).

Read the original input: '3.14' is a string containing a decimal value.

Convert the string to a float: float('3.14') produces the decimal value 3.14.

Convert 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

  • Assuming int() rounds to the nearest integer.

    int() truncates the decimal portion, even when that portion is very large.

    Fix: Expect the decimal portion to be removed, so 3.99999 becomes 3.

  • Passing a decimal string directly to int().

    The source identifies decimal strings as inputs that raise an error.

    Fix: Convert the string to a float first, then pass the float to int(): int(float('3.14')).

  • Treating int(), float(), and str() as interchangeable.

    The functions represent different conversion paths, and int() changes a decimal value into an integer by truncation.

    Fix: Choose the function according to the intended result: integer conversion, float conversion, or string conversion.

Practice the Rule

EASY

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.
  1. Use 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(), and str() represent different conversion intentions and should not be treated as interchangeable.