Swift
Programming
Thousand Separator
Integer Formatting
Code Tutorial

Adding Thousand Separator to Int in Swift

Master System Design with Codemia

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

Introduction

Adding a thousands separator to an Int in Swift is primarily a formatting task, not a numeric one. The best default solution is NumberFormatter because it understands locale rules, negative numbers, and grouping automatically.

That matters more than it first appears. A hard-coded comma might look correct on one device and wrong on another because grouping separators are locale-dependent.

Use NumberFormatter for standard formatting

Foundation provides NumberFormatter for exactly this job:

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

On a U.S. locale this typically prints 1,234,567. In other locales it may use a different grouping separator, which is usually what you want in a user-facing app.

Reuse the formatter when formatting many values

NumberFormatter is more expensive than plain string interpolation, so you should usually create it once and reuse it.

swift
1import Foundation
2
3enum Formatters {
4    static let decimal: NumberFormatter = {
5        let formatter = NumberFormatter()
6        formatter.numberStyle = .decimal
7        return formatter
8    }()
9}
10
11let values = [1000, 25000, 9876543]
12
13for value in values {
14    print(Formatters.decimal.string(from: NSNumber(value: value)) ?? "\(value)")
15}

This pattern is especially useful in table views, collection views, and SwiftUI lists where numbers are formatted repeatedly.

Wrap the behavior in an extension

If you want a convenient API, add a computed property on Int that uses a shared formatter.

swift
1import Foundation
2
3enum SharedFormatter {
4    static let decimal: NumberFormatter = {
5        let formatter = NumberFormatter()
6        formatter.numberStyle = .decimal
7        return formatter
8    }()
9}
10
11extension Int {
12    var withThousandsSeparator: String {
13        SharedFormatter.decimal.string(from: NSNumber(value: self)) ?? "\(self)"
14    }
15}
16
17print(1200000.withThousandsSeparator)

This keeps view code tidy without recreating the formatter each time.

Override grouping only when the requirement is fixed

Sometimes the app must display a fixed separator regardless of locale. In that case, you can customize the formatter:

swift
1import Foundation
2
3let formatter = NumberFormatter()
4formatter.numberStyle = .decimal
5formatter.usesGroupingSeparator = true
6formatter.groupingSeparator = "_"
7
8print(formatter.string(from: 1234567 as NSNumber) ?? "")

That produces something like 1_234_567.

Use this only when the business rule truly requires a fixed display format. Otherwise, locale-aware output is the better user experience.

Avoid manual string insertion unless necessary

It is possible to build a custom algorithm that inserts commas every three digits, but that is rarely the best production solution. Manual formatting often misses edge cases such as:

  • negative numbers
  • alternate locale separators
  • non-decimal styles
  • future reuse for Double or currency values

If the requirement is simply "show this number in a human-readable grouped format", NumberFormatter already solves the hard parts.

Common Pitfalls

The most common mistake is hard-coding commas when the app should respect the user's locale. That produces output that feels incorrect or inconsistent on international devices.

Another issue is creating a new NumberFormatter every time a cell renders. That works functionally but wastes work in hot UI paths.

Developers also sometimes treat thousands separators as part of the underlying number. They are not. Grouping is a presentation concern, so keep it in formatting code rather than mixing it into storage or business logic.

Finally, if you override the separator manually, remember that you are taking responsibility for that display rule across the app.

Summary

  • Use NumberFormatter with .decimal for most thousands-separator formatting in Swift.
  • Reuse formatter instances when numbers are formatted often.
  • Wrap formatting in an extension or shared helper for cleaner call sites.
  • Override the grouping separator only when a fixed custom format is truly required.
  • Treat thousands separators as display logic, not as part of the numeric value.

Course illustration
Course illustration

All Rights Reserved.