We use Int, Double, and Float to store numbers in Swift. Each type helps us keep different kinds of numbers, like whole numbers or numbers with decimals.
Int, Double, Float number types in Swift
var wholeNumber: Int = 10 var decimalNumber: Double = 3.14 var smallerDecimal: Float = 2.5
Int stores whole numbers without decimals.
Double stores decimal numbers with high precision.
Float stores decimal numbers but with less precision and uses less memory.
var age: Int = 25
var pi: Double = 3.14159
var temperature: Float = 36.6
var count = 100 // Swift infers this as Int
This program counts apples and calculates total cost using Int and Double types. It shows how to convert Int to Double for calculation.
import Foundation var apples: Int = 5 var pricePerApple: Double = 0.99 var totalCost: Double = Double(apples) * pricePerApple print("You bought \(apples) apples.") print("Each apple costs $\(pricePerApple).") print("Total cost is $\(totalCost).")
You cannot directly multiply Int and Double without converting one type to the other.
Double is preferred for most decimal numbers because it is more precise than Float.
Use Int when you only need whole numbers to save memory and avoid decimals.
Int stores whole numbers.
Double stores precise decimal numbers.
Float stores decimal numbers with less precision and uses less memory.
Practice
Which Swift type is best to store the number 42 without any decimal part?
Solution
Step 1: Understand the number type
The number 42 is a whole number without decimals.Step 2: Match with Swift types
Int stores whole numbers, while Double and Float store decimal numbers.Final Answer:
Int -> Option DQuick Check:
Whole number = Int [OK]
- Choosing Double or Float for whole numbers
- Confusing String with number types
Which of the following is the correct way to declare a variable price as a Double with value 19.99 in Swift?
?
Solution
Step 1: Check type and value compatibility
Double can store decimal numbers like 19.99 correctly.Step 2: Validate syntax
var price: Double = 19.99 uses correct syntax: variable name, type annotation, and decimal value.Final Answer:
var price: Double = 19.99 -> Option CQuick Check:
Double declaration syntax = var name: Double = value [OK]
- Using 'f' suffix which is invalid in Swift
- Assigning decimal to Int type
- Using quotes for numbers
What will be the output of this Swift code?
let a: Float = 3.14 let b: Double = 3.14 print(a == b)
Solution
Step 1: Understand type comparison
Float and Double are different types; comparing them directly requires conversion.Step 2: Check equality operation
Swift allows comparing Float and Double, but values may differ due to precision, so a == b is false.Final Answer:
false -> Option AQuick Check:
Float vs Double equality = false [OK]
- Assuming Float and Double are equal if values look same
- Expecting compile error for comparing different types
Identify the error in this Swift code snippet:
var x: Int = 10.5 var y: Float = 20
Solution
Step 1: Check variable type and assigned value
Variable x is Int but assigned 10.5 which is a decimal number.Step 2: Validate other assignments
Variable y is Float and assigned 20 (an integer), which is allowed by implicit conversion.Final Answer:
Cannot assign decimal 10.5 to Int variable -> Option BQuick Check:
Int cannot hold decimals [OK]
- Thinking Float cannot hold integer values
- Expecting semicolons in Swift
- Assuming variable names are invalid
You want to store the value 0.123456789 in Swift with the highest precision possible. Which type should you choose and why?
Solution
Step 1: Understand precision needs
The number 0.123456789 has many decimal places requiring high precision.Step 2: Compare Swift types for precision
Double stores decimal numbers with more precision than Float; Int stores whole numbers only.Step 3: Consider String
String stores text, not numbers, so it cannot be used for numeric calculations.Final Answer:
Double, because it stores decimal numbers with more precision -> Option AQuick Check:
Highest decimal precision = Double [OK]
- Choosing Float for high precision decimals
- Using Int for decimal numbers
- Using String for numeric calculations
Ready to prove it?
Pass the test and this pattern moves unknown
