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 localeThe 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))))