how to add commas to a number in swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
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:
If the number represents money, switch to .currency instead of manually adding symbols or separators yourself.
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:
You can also specify locale-aware precision options:
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
NumberFormatterwhen 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.
Related reading
- How to add constraints programmatically using Swift
- How to add constraints programmatically using Swift
- How to add dividers and spaces between items in RecyclerView
- How to add dividers and spaces between items in RecyclerView
- How to add Done button to Numpad in iOS using Swift?
- How to add HeaderView in UICollectionView like UITableView's tableHeaderView
- How to add leading padding to view added inside an UIStackView
- How to add leading padding to view added inside an UIStackView
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.