Swift
Int to String
Swift Programming
Data Conversion
Swift Language

Convert Int to String in Swift

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The normal way to convert an Int to String in Swift is String(number). It is explicit, readable, and works well for everyday cases such as labels, logs, and URLs.

There are other ways to get string output, but they solve slightly different problems. The best choice depends on whether you want a plain conversion, string interpolation, or formatted output.

The Direct Conversion

This is the standard answer:

swift
let number = 123
let text = String(number)
print(text)

text is now a real String, so you can pass it to APIs that specifically expect a string value.

This is the best default when you are converting an Int as data rather than building a sentence around it.

String Interpolation Is Not Quite the Same

If you are only embedding the value inside a larger string, interpolation is often cleaner:

swift
let count = 42
let message = "You have \(count) unread messages."
print(message)

You do not need to call String(count) first unless another API specifically wants the converted string separately.

So a useful rule is:

  • use String(number) when you need a string value
  • use interpolation when you are already building a larger string

description Also Produces a String

Integers also expose description:

swift
let number = 99
let text = number.description
print(text)

This works because Int conforms to string-conversion protocols, but in most ordinary Swift code, String(number) is clearer to readers who are scanning for an explicit type conversion.

Formatted Output Is a Different Problem

Sometimes you do not just want "123". You want padding, grouping, or localization. For example:

swift
1import Foundation
2
3let number = 7
4let padded = String(format: "%03d", number)
5print(padded)   // 007

That is not better for plain conversion, but it is useful when formatting matters.

For user-facing text, NumberFormatter is often the better tool because it handles locale-aware formatting:

swift
1import Foundation
2
3let formatter = NumberFormatter()
4formatter.numberStyle = .decimal
5
6let number = 1234567
7let text = formatter.string(from: NSNumber(value: number)) ?? ""
8print(text)

This is the right direction when the output should respect local conventions such as grouping separators.

Common Real Uses

Converting an Int to String shows up in a few common places:

  • updating labels in UIKit or SwiftUI
  • building file names or URL components
  • logging numeric values
  • serializing values into plain-text formats

Example:

swift
let userId = 42
let endpoint = "/users/" + String(userId)
print(endpoint)

That is a genuine conversion case. If you were only printing it, interpolation would probably be simpler.

Avoid Premature Formatting Tricks

Some developers reach for String(format:) even when they only need a direct conversion:

swift
let text = String(format: "%d", 123)

That works, but it is more verbose than necessary and introduces formatting syntax that is easy to misuse. Prefer the simple initializer unless you actually need formatting behavior.

Common Pitfalls

The most common mistake is converting too early. If you only need the number inside a sentence, interpolation is often cleaner than storing an extra string variable.

Another mistake is using String(format:) for ordinary conversions. It is fine for padded or formatted output, but overkill for basic cases.

People also forget about localization. A plain integer-to-string conversion is not the same as user-friendly number formatting in every locale.

Finally, keep the distinction between data and presentation clear. String(number) converts data. NumberFormatter controls presentation.

Summary

  • Use String(number) for the normal Int to String conversion.
  • Use interpolation when the integer is part of a larger string.
  • 'description also works, but String(number) is usually clearer.'
  • Use String(format:) only when you need formatting such as zero-padding.
  • Use NumberFormatter for user-facing localized number formatting.

Course illustration
Course illustration

All Rights Reserved.