3 min readMar 15, 2026
SwiftHow to Format Numbers
Use NumberFormatter for readable output.
Steps
- Paste the code into a Swift file or playground.
- Run it once to verify the output.
- Adjust the inputs to match your use case.
Copy and paste
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
let value = 12345.678
let text = formatter.string(from: NSNumber(value: value))
print(text ?? "")Notes
- Keep functions small and focused for reuse.
- Prefer safe APIs like optionals and guards.
