MasterOnce
HomeBlogHow-To
3 min readMar 15, 2026
Swift

How to Format Numbers

Use NumberFormatter for readable output.

Steps

  1. Paste the code into a Swift file or playground.
  2. Run it once to verify the output.
  3. 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.