0Pricing
Swift Academy · Lesson

Locale-Aware Formatting

Format numbers, dates, and currencies per locale.

Numbers Are Not Universal

The number one million two hundred thousand reads as 1,200,000.50 in the US, 1.200.000,50 in Germany, and 12,00,000.50 in India. Hardcoding separators or formats breaks for most of the world. Swift's FormatStyle API handles this correctly per locale.

import Foundation
let value = 1_200_000.5
print(value.formatted()) // uses current locale

The formatted() Entry Point

Most standard types — numbers, dates, measurements, lists — have a formatted() method. With no arguments it uses sensible locale-aware defaults; pass a style to customize.

import Foundation
let n = 1234.567
print(n.formatted())                      // default
print(n.formatted(.number.precision(.fractionLength(2))))

All lessons in this course

  1. String Catalogs and Localized Strings
  2. Pluralization and Stringsdict
  3. Locale-Aware Formatting
  4. Right-to-Left and Layout Direction
← Back to Swift Academy