Skip to main content
Logo image

Section 6.2 Converting Integers to Floats

Casting an integer to a float effectively changes the data type of the stored value from an int to a foat. Whereas the integer 5 is represented in the computer’s memory using binary 2’s complement for example, when you write (float)5 this resulting number is stored as mantissa and exponent in the way we just learned.

Activity 6.1.

What do you think happens the the value of the number when you cast an int to a float? Are digits beyond the decimal point added on?

admin.....open in new window

Please paste your text submission into the box below, then select Run to submit it:

Check Your Understanding Check Your Understanding

1.

Does the datatype of a variable change when you cast or is it just the number itself that is stored differently inside the computer? For example, if \(a\) and \(b\) are variables of type int, where a has the value 12, b has the value 4, what is the data type of the variable a after the operation ((float)a+b)/3 is performed?
  • int
  • Correct!
  • float
  • Not quite - try again!
  • long
  • Not quite - try again!
  • floating int
  • Not quite - try again!
Hint.
Does the data type of a change in this process?