Concepts / Working with Floating-Point Numbers

Working with Floating-Point Numbers

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

  • Programming

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?

  • 3
  • 4
  • An error
Reveal answer

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

int()int()3.99999positive floating-pointvalue3decimal portion removed-3.99999negative floating-pointvalue-3decimal portion removed
What happens to the decimal portion when int() converts positive and negative floating-point values?

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.

isisisisint()int()int()int()Input valueFloating-point valueconvertibleIntegerconversion succeedsWhole-number stringconvertibleConversion errorconversion failsDecimal stringerrorInvalid inputerror
Which input values pass through int() successfully, and which inputs cause a conversion error?
Input categoryint() result
Floating-point valueConverts to an integer by truncating the decimal portion
Whole-number stringConverts to an integer
Decimal stringRaises an error
Invalid inputRaises 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?

Recognize the input form: The value is a decimal string, so int() cannot convert it directly.

Convert the string to a floating-point value: Use float('3.14') as the first conversion step.

Convert 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.

FunctionRole 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() directly: errorfloat()then int()str() is a different path'3.14'decimal string3integer after supportedconversion path3.14floating-point intermediatestring conversionnot the direct integerconversion described here
How does the same conversion task differ when int(), float(), or str() is involved?

Mistakes to Avoid

  • Assuming int() rounds to the nearest integer.

    int() truncates by removing the decimal portion, regardless of how large that portion is.

    Fix: Expect 3.99999 to become 3.

  • Passing a decimal string directly to int().

    int() works with whole-number strings, but a decimal string causes an error.

    Fix: Convert the decimal string to a floating-point value first, then convert that result to an integer.

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

    The functions have different roles in the conversion process described here.

    Fix: Use int() when the target is an integer, and use the float-then-int path for a decimal string.

Check Your Understanding

MEDIUM

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() before int().
  • Invalid input raises an error.
  1. int() converts supported values to integers.
  2. When given a floating-point value, int() truncates by removing the decimal portion rather than rounding.
  3. Floating-point values and whole-number strings can be converted directly, while decimal strings and invalid input cause an error.
  4. A decimal string can be converted by using float() first and int() second, as in int(float('3.14')).
  5. int(), float(), and str() 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(), or str() according to the conversion role required.