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:
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.
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.
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:
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
Doubleor 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
NumberFormatterwith.decimalfor 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.

