Bird
Raised Fist0
Swiftprogramming~10 mins

Int, Double, Float number types in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Int, Double, Float number types
Start
↓
Declare Int variable
↓
Declare Double variable
↓
Declare Float variable
↓
Use variables in calculations
↓
Print results
↓
End
This flow shows declaring Int, Double, and Float variables, using them, and printing their values.
Execution Sample
Swift
let age: Int = 30
let price: Double = 19.99
let temperature: Float = 36.6
print(age)
print(price)
print(temperature)
This code declares three variables of different number types and prints their values.
Execution Table
StepActionVariableValueOutput
1Declare Int variable 'age'age30
2Declare Double variable 'price'price19.99
3Declare Float variable 'temperature'temperature36.6
4Print 'age'age3030
5Print 'price'price19.9919.99
6Print 'temperature'temperature36.636.6
💡 All variables declared and printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
ageundefined30303030
priceundefinedundefined19.9919.9919.99
temperatureundefinedundefinedundefined36.636.6
Key Moments - 2 Insights
Why do we use Int for 'age' but Double for 'price'?
Because 'age' is a whole number without decimals, Int is perfect. 'price' can have decimals, so Double is used. See steps 1 and 2 in execution_table.
Why is 'temperature' declared as Float and not Double?
Float uses less memory and is enough for 'temperature' precision here. Double is more precise but uses more space. See step 3 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'price' after step 2?
A19.99
B30
C36.6
Dundefined
💡 Hint
Check the 'price' value in row with step 2 in execution_table.
At which step is the 'temperature' variable declared?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the action 'Declare Float variable' in execution_table.
If we change 'age' to 30.5, what will happen?
AIt will convert to Double silently.
BIt will store 30.5 as Int automatically.
CIt will cause a type error because Int cannot hold decimals.
DIt will store 30.5 as Float.
💡 Hint
Recall that Int only holds whole numbers, see step 1 in execution_table.
Concept Snapshot
Int, Double, Float in Swift:
- Int: whole numbers, no decimals
- Double: decimal numbers, high precision
- Float: decimal numbers, less precision, uses less memory
Use Int for counts, Double for money, Float for less precise decimals
Declare with let/var and type annotation
Full Transcript
This visual execution shows how Swift handles three number types: Int, Double, and Float. First, an Int variable 'age' is declared with value 30, suitable for whole numbers. Next, a Double variable 'price' is declared with 19.99, allowing decimals with high precision. Then, a Float variable 'temperature' is declared with 36.6, which uses less memory but less precision. Each variable is printed, showing their stored values. Key points include choosing Int for whole numbers, Double for precise decimals, and Float for less precise decimals. Trying to assign a decimal to Int causes an error. This helps beginners understand number types and their uses in Swift.

Practice

(1/5)
1.

Which Swift type is best to store the number 42 without any decimal part?

easy
A. String
B. Double
C. Float
D. Int

Solution

  1. Step 1: Understand the number type

    The number 42 is a whole number without decimals.
  2. Step 2: Match with Swift types

    Int stores whole numbers, while Double and Float store decimal numbers.
  3. Final Answer:

    Int -> Option D
  4. Quick Check:

    Whole number = Int [OK]
Hint: Whole numbers use Int, decimals use Double or Float [OK]
Common Mistakes:
  • Choosing Double or Float for whole numbers
  • Confusing String with number types
2.

Which of the following is the correct way to declare a variable price as a Double with value 19.99 in Swift?

?
easy
A. var price: Int = 19.99
B. var price = 19.99f
C. var price: Double = 19.99
D. var price: Float = "19.99"

Solution

  1. Step 1: Check type and value compatibility

    Double can store decimal numbers like 19.99 correctly.
  2. Step 2: Validate syntax

    var price: Double = 19.99 uses correct syntax: variable name, type annotation, and decimal value.
  3. Final Answer:

    var price: Double = 19.99 -> Option C
  4. Quick Check:

    Double declaration syntax = var name: Double = value [OK]
Hint: Use var name: Double = value for decimals [OK]
Common Mistakes:
  • Using 'f' suffix which is invalid in Swift
  • Assigning decimal to Int type
  • Using quotes for numbers
3.

What will be the output of this Swift code?

let a: Float = 3.14
let b: Double = 3.14
print(a == b)
medium
A. false
B. true
C. Compile-time error
D. Runtime error

Solution

  1. Step 1: Understand type comparison

    Float and Double are different types; comparing them directly requires conversion.
  2. Step 2: Check equality operation

    Swift allows comparing Float and Double, but values may differ due to precision, so a == b is false.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    Float vs Double equality = false [OK]
Hint: Float and Double differ in precision; direct equality often false [OK]
Common Mistakes:
  • Assuming Float and Double are equal if values look same
  • Expecting compile error for comparing different types
4.

Identify the error in this Swift code snippet:

var x: Int = 10.5
var y: Float = 20
medium
A. Cannot assign integer 20 to Float variable
B. Cannot assign decimal 10.5 to Int variable
C. Missing semicolon after variable declarations
D. Variable names are invalid

Solution

  1. Step 1: Check variable type and assigned value

    Variable x is Int but assigned 10.5 which is a decimal number.
  2. Step 2: Validate other assignments

    Variable y is Float and assigned 20 (an integer), which is allowed by implicit conversion.
  3. Final Answer:

    Cannot assign decimal 10.5 to Int variable -> Option B
  4. Quick Check:

    Int cannot hold decimals [OK]
Hint: Int variables cannot hold decimal values [OK]
Common Mistakes:
  • Thinking Float cannot hold integer values
  • Expecting semicolons in Swift
  • Assuming variable names are invalid
5.

You want to store the value 0.123456789 in Swift with the highest precision possible. Which type should you choose and why?

hard
A. Double, because it stores decimal numbers with more precision
B. Float, because it uses less memory
C. Int, because it stores whole numbers accurately
D. String, to keep exact digits

Solution

  1. Step 1: Understand precision needs

    The number 0.123456789 has many decimal places requiring high precision.
  2. Step 2: Compare Swift types for precision

    Double stores decimal numbers with more precision than Float; Int stores whole numbers only.
  3. Step 3: Consider String

    String stores text, not numbers, so it cannot be used for numeric calculations.
  4. Final Answer:

    Double, because it stores decimal numbers with more precision -> Option A
  5. Quick Check:

    Highest decimal precision = Double [OK]
Hint: Use Double for precise decimal numbers [OK]
Common Mistakes:
  • Choosing Float for high precision decimals
  • Using Int for decimal numbers
  • Using String for numeric calculations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard