Explicit Numeric Conversions
Convert between numeric types deliberately to avoid errors.
Explicit Numeric Conversions is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Swift Requires Explicit Conversion
Swift does not automatically convert between numeric types. To combine an Int and a Double, you must convert one explicitly.
let count = 3
let price = 2.5
let total = Double(count) * price
print(total)Why No Implicit Conversion
Implicit conversions can silently lose data or change meaning. Swift makes you state intent, preventing subtle bugs.
let whole = 7
let fraction = 0.5
let result = Double(whole) + fraction
print(result)Converting Int to Double
Wrap an integer in Double(...) to get its floating-point form. This is needed before real division.
let a = 7
let b = 2
let ratio = Double(a) / Double(b)
print(ratio)Converting Double to Int
Int(...) on a Double truncates toward zero, discarding the fractional part (not rounding).
let value = 3.99
print(Int(value))
print(Int(-3.99))Rounding Before Converting
If you want rounding rather than truncation, call .rounded() first, then convert to Int.
let value = 3.6
let rounded = Int(value.rounded())
print(rounded)Converting Between Integer Types
Even integer-to-integer needs explicit conversion, like Int(small) or Int8(big), because ranges differ.
let small: Int8 = 100
let big: Int = Int(small)
print(big + 1000)Conversion That Overflows
Converting a value that does not fit the target type traps at runtime. Validate the range first when unsure.
let n = 200
if n <= Int(Int8.max) {
print("fits")
} else {
print("would overflow Int8")
}Failable Numeric Initializers
Some conversions use failable initializers like Int8(exactly:) which return an optional, returning nil when the value does not fit exactly.
let ok = Int8(exactly: 100)
let bad = Int8(exactly: 300)
print(ok as Any)
print(bad as Any)String to Number Conversion
Converting a String to a number is also explicit and failable, returning an optional in case the text is not a valid number.
let text = "42"
if let n = Int(text) {
print("Parsed:", n)
}
print(Int("hello") as Any)Combining Mixed Numeric Math
A typical pattern: convert all operands to a common type, do the math, then convert the final result back if needed.
let items = 5
let pricePerItem = 1.99
let total = Double(items) * pricePerItem
let rounded = (total * 100).rounded() / 100
print("Total:", rounded)Conversion Keeps Code Safe
Explicit conversions document where precision changes happen, making numeric code easier to audit and reason about.
let measured = 9.81
let asInt = Int(measured)
let backToDouble = Double(asInt)
print(measured, asInt, backToDouble)Quick Check
Predict the result of converting a Double to Int.
Recap: Explicit Numeric Conversions
You learned that Swift never converts numeric types implicitly, that Int(...) truncates Doubles while Double(...) widens Ints, and that failable initializers like Int8(exactly:) and Int("text") return optionals when a conversion cannot succeed.
let pieces = 7
let share = Double(pieces) / 2.0
print("Share:", share)
print("Truncated:", Int(share))Frequently asked questions
Is the “Explicit Numeric Conversions” lesson free?
Yes — the full text of “Explicit Numeric Conversions” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Explicit Numeric Conversions”?
Convert between numeric types deliberately to avoid errors. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Explicit Numeric Conversions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Integer Types and Their Ranges
- Floating-Point: Double vs Float
- Explicit Numeric Conversions
- Numeric Literals and Underscores