Swift
Programming
Number Formatting
Code Tutorial
iOS Development

how to add commas to a number in swift?

Master System Design with Codemia

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

Introduction

In Swift, adding commas to a number really means formatting it with the locale's grouping separators. The most common solution is NumberFormatter, and in newer Swift versions you can also use the formatted API for a more fluent style.

Use NumberFormatter for Broad Compatibility

NumberFormatter is the classic Foundation solution and still the most flexible option when you need localization control.

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

On a locale that uses commas for grouping, this prints 1,234,567. On other locales, the grouping separator may be different, which is usually the correct behavior for user-facing text.

That point matters because formatting is not really about forcing commas. It is about presenting numbers in the style users expect in their region.

Format Decimals and Currency Carefully

The same formatter can handle decimals as well:

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

If the number represents money, switch to .currency instead of manually adding symbols or separators yourself.

swift
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "en_US")
print(formatter.string(from: NSNumber(value: 1234567.89)) ?? "")

Manual string hacks are tempting for simple cases, but they break quickly once decimals, negative values, or localization enter the picture.

Use Swift's Modern Formatting API

In newer Swift, the formatting API on numeric types is often more concise:

swift
let value = 1234567
let text = value.formatted(.number)
print(text)

You can also specify locale-aware precision options:

swift
1let value = 1234567.891
2let text = value.formatted(
3    .number
4        .precision(.fractionLength(0...2))
5)
6print(text)

This style reads nicely and avoids creating a separate formatter object for simple formatting tasks.

When to Store Strings and When Not To

Keep numeric values as numbers in your model layer and format them only when presenting them to the user. If you convert to a formatted string too early, sorting, arithmetic, and serialization become harder than they need to be.

That means the formatter belongs near the UI or output boundary, not in the core business logic unless the formatted text itself is the actual product.

This separation also makes testing easier. Your business logic can operate on Int, Double, or Decimal, while UI tests or formatter tests verify that presentation uses the correct grouping and locale rules.

It also prevents subtle bugs in APIs and persistence layers. A formatted string such as 1,234 looks helpful to a person, but it is the wrong representation to send to a database or JSON API that expects a numeric value.

The general rule is simple: numbers stay numeric until the moment you need human-readable output. Formatting is a presentation concern, and Swift gives you strong tools for handling that boundary correctly.

Common Pitfalls

  • Building grouped numbers with manual string slicing instead of a formatter.
  • Assuming commas are always the correct separator in every locale.
  • Storing formatted strings instead of the original numeric values.
  • Using decimal formatting for currency instead of a currency-aware style.
  • Forgetting that NumberFormatter.string(from:) returns an optional.

Summary

  • Use NumberFormatter when you need flexible, locale-aware number formatting.
  • Use formatted(.number) for concise modern Swift code.
  • Let the locale choose the correct grouping separator for user-facing output.
  • Keep values numeric internally and format them only for display.
  • Prefer formatter APIs over manual string manipulation.

Course illustration
Course illustration

All Rights Reserved.