← Back to Swift Course | Chapter 2: Variables & Types | Lesson 8 of 8

Type Conversion

Type conversion means turning one kind of value, like a number, into a different kind, like text, so different parts of your program can work together.
Syntax
swift
let asDouble = Double(intValue)
let asInt = Int(stringValue)       // returns Int? (optional)
let asString = String(number)

Numeric Type Conversion

To convert between numeric types like Int and Double, you wrap the value in the target type's initializer -- Swift never converts numbers implicitly.

Example: Numeric Type Conversion

markup
// Declare a constant `wholeNumber`, set to `7`
let wholeNumber = 7
// Declare a constant `asDouble`, set to `Double(wholeNumber)`
let asDouble = Double(wholeNumber)
// Print "Int: \(wholeNumber), as Double: \(asDouble)" to the console
print("Int: \(wholeNumber), as Double: \(asDouble)")

Converting Strings to Numbers

Converting a String to a number uses a failable initializer that returns an optional, since not every string is a valid number.

Example: Converting Strings to Numbers

markup
// Declare a constant `validText`, set to `"42"`
let validText = "42"
// Declare a constant `invalidText`, set to `"hello"`
let invalidText = "hello"
// Declare a constant `validNumber`, set to `Int(validText)`
let validNumber = Int(validText)
// Declare a constant `invalidNumber`, set to `Int(invalidText)`
let invalidNumber = Int(invalidText)
// Print "Valid: \(String(describing: validNumber))" to the console
print("Valid: \(String(describing: validNumber))")
// Print "Invalid: \(String(describing: invalidNumber))" to the console
print("Invalid: \(String(describing: invalidNumber))")

Converting Numbers to Strings

Turning a number into a String is done either with the String(_:) initializer or through string interpolation, both producing the same text representation.

Example: Converting Numbers to Strings

markup
// Declare a constant `count`, set to `100`
let count = 100
// Declare a constant `asText`, set to `String(count)`
let asText = String(count)
// Print `"Converted: " + asText` to the console
print("Converted: " + asText)
// Print "Interpolated: \(count)" to the console
print("Interpolated: \(count)")

Truncation When Converting Double to Int

Converting a Double to an Int truncates the fractional part rather than rounding, so 4.9 becomes 4, not 5.

Note: Use .rounded() before converting if you want rounding instead of truncation.

Example: Truncation When Converting Double to Int

markup
// Declare a constant `price`, set to `4.9`
let price = 4.9
// Declare a constant `truncated`, set to `Int(price)`
let truncated = Int(price)
// Print "4.9 truncated to Int: \(truncated)" to the console
print("4.9 truncated to Int: \(truncated)")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to convert a non-numeric string to Int, e.g. Int("hello"), without handling the resulting nil from the failable initializer.
  2. Assuming numeric conversions like Int to Double happen automatically; Swift always requires an explicit conversion call.
  3. Forgetting that converting a Double to Int truncates the decimal part rather than rounding it.
Chapter Summary
  • Converting between numeric types requires an explicit initializer call, like Double(someInt).
  • Converting a String to a number uses a failable initializer, e.g. Int("42"), which returns an optional.
  • Converting a number to a String is done with String(someNumber) or interpolation.
  • Int(someDouble) truncates toward zero rather than rounding.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.