Concepts / String Manipulation and Conversion

String Manipulation and Conversion

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

  • Programming

A Decimal Does Not Round Up

Suppose a program receives the value 7.9 but needs an integer. Applying int() does not ask whether 7.9 is closer to 7 or 8. Instead, int() removes the decimal portion. The result is 7. This behavior is called truncation.

What do you think happens?

What values result when int() is applied to 7.9 and -7.9?

  • 8 and -8
  • 7 and -7
  • 7 and -8
Reveal answer

Answer: 7 and -7

int() removes the decimal portion from each value. It does not round to the nearest integer.

Following the Truncation Step

int()int()7.97-7.9-7
What changes when int() removes the decimal portion from positive and negative values?

Truncation removes the decimal part entirely, regardless of how large that decimal part is. Therefore, 3.99999 becomes 3, not 4. The same removal rule applies to a negative decimal such as -7.9, producing -7 rather than -8. The operation is not nearest-integer rounding.

Tracking the Decimal Portion

Determine the result of int(3.99999).

Start with the value: The value is 3.99999, which has an integer portion of 3 and a decimal portion of .99999.

Apply int(): int() removes the decimal portion instead of rounding based on its size.

Read the result: The remaining integer portion is 3.

int(3.99999) produces 3, not 4.

Accepted and Rejected Inputs

int() works with integer values, floating-point values, and strings that contain whole numbers. It raises an error when given a string containing a decimal value or other invalid input. The important distinction is between a decimal number and a decimal string: a decimal number can be truncated directly, while a string that includes decimal notation cannot be passed directly to int().

value typevalue typevalue typevalue typeaccepteddecimal removedacceptedrejectedInput to int()IntegerInteger resultDecimal numberTruncated integerWhole-number stringErrorDecimal string
What happens next when int() receives an integer, a decimal number, a whole-number string, or a decimal string?
Input categoryint() behavior
Integer valueConverts to an integer
Decimal numberRemoves the decimal portion
Whole-number stringConverts to an integer
Decimal stringRaises an error
Invalid inputRaises an error

The input's form determines whether int() succeeds and whether truncation is needed.

Converting Decimal Text

A string containing a decimal, such as the text 3.14, cannot be converted directly to an integer with int(). To convert that decimal string, first use float() and then pass the resulting value to int(). The conversion path is int(float('3.14')). The inner float() conversion handles the decimal string; the outer int() conversion then truncates the decimal portion.

float()int() truncates3.143.143
How does a decimal string become an integer when direct int() conversion is not available?

Reading int(float('3.14'))

Explain the conversion sequence in int(float('3.14')).

Read the inner conversion: float('3.14') converts the decimal string into a floating-point value.

Read the outer conversion: int() receives that decimal value and removes its decimal portion.

State the result: The final integer is 3.

int(float('3.14')) produces 3.

Comparing Conversion Paths

integer conversiondecimal conversionstring conversionint()3float()3.14str()text
How do the conversion functions differ when a decimal string must become an integer?

These functions represent different conversion intentions. int() produces an integer and, for decimal numbers, removes the decimal portion. float() is the intermediate conversion used in the source example to interpret the decimal string 3.14 before int() is applied. str() belongs to the string-conversion side of the comparison rather than the truncation step. When the goal is specifically to turn decimal text into an integer, the relevant path is float() followed by int().

Mistakes to Avoid

  • Assuming int() rounds to the nearest integer

    int() truncates by removing the decimal portion, regardless of its magnitude.

    Fix: Treat int(3.99999) as 3.

  • Treating a decimal string like a decimal number

    int() works with whole-number strings, but a decimal string is an input form that raises an error.

    Fix: Use int(float('3.14')) when converting the decimal string described in the source.

  • Ignoring the order of nested conversions

    The inner float() conversion handles the decimal string first, and the outer int() conversion performs truncation afterward.

    Fix: Trace the expression from the inside out: decimal string to float, then float to integer.

Before using int(), identify both the input's form and the behavior you want. If the input is a decimal number, expect truncation rather than rounding. If the input is decimal text, use the two-step conversion shown in the source: first float(), then int().

Check Your Prediction

MEDIUM

For each expression, predict whether it produces an integer result or raises an error. Then explain whether truncation occurs: int(8), int(8.75), int('8'), int('8.75'), and int(float('8.75')).

Hints
  • Separate numeric values from strings.
  • A whole-number string is different from a decimal string.
  • For nested conversion, evaluate the inner float() step before the outer int() step.
  • Remember that int() removes the decimal portion rather than rounding.
  1. int() converts values to integers by truncating decimal portions instead of rounding. It can handle integer values, decimal numbers, and whole-number strings. A decimal string or other invalid input raises an error. To convert decimal text such as '3.14' to an integer, first use float() and then int().

Key Takeaways

  • int() removes the decimal portion rather than rounding.
  • Values such as 3.99999 become 3.
  • int() accepts decimal numbers and whole-number strings, but decimal strings and invalid input raise an error.
  • A decimal string can be converted through int(float('3.14')).
  • The order of nested conversions matters: float() handles the decimal string before int() truncates the result.