--- name: swift-formatstyle description: "Format values for display using the FormatStyle protocol and its concrete types. Use when formatting numbers (integers, floating-point, decimals), currencies, percentages, dates, date ranges, relative dates, durations (Duration.TimeFormatStyle, Duration.UnitsFormatStyle), measurements, person names (PersonNameComponents.FormatStyle), byte counts (ByteCountFormatStyle), lists (ListFormatStyle), and URLs (URL.FormatStyle). Also covers creating custom FormatStyle conformances and replacing legacy Formatter subclasses. FormatStyle is available iOS 15+; Duration styles require iOS 16+." --- # Swift FormatStyle Format values for human-readable display using the `FormatStyle` protocol and Foundation's concrete format styles. Replaces legacy `Formatter` subclasses with a type-safe, composable, cacheable API. Docs: [FormatStyle](https://sosumi.ai/documentation/foundation/formatstyle) ## Contents - [Quick Reference](#quick-reference) - [Numbers](#numbers) - [Currency](#currency) - [Percentages](#percentages) - [Dates](#dates) - [Durations](#durations) - [Measurements](#measurements) - [Person Names](#person-names) - [Lists](#lists) - [Byte Counts](#byte-counts) - [URLs](#urls) - [SwiftUI Integration](#swiftui-integration) - [Custom FormatStyle](#custom-formatstyle) - [Common Mistakes](#common-mistakes) - [Review Checklist](#review-checklist) ## Quick Reference | Type | Style Access | Example | |------|-------------|---------| | `Int`, `Double` | `.number` | `42.formatted(.number.precision(.fractionLength(2)))` → `"42.00"` | | Currency | `.currency(code:)` | `29.99.formatted(.currency(code: "USD"))` → `"$29.99"` | | Percent | `.percent` | `0.85.formatted(.percent)` → `"85%"` | | `Date` | `.dateTime` | `Date.now.formatted(.dateTime.month().day().year())` | | Date range | `.interval` | `(date1...Currency(code: "USD") .precision(.fractionLength(0)) 1234.56.formatted(style) // "$1,235" ``` ## Percentages ```swift 0.85.formatted(.percent) // "85%" 0.8567.formatted(.percent.precision(.fractionLength(1))) // "85.7%" 42.formatted(.percent) // "42%" (integer) ``` ## Dates ```swift let now = Date.now // Components now.formatted(.dateTime.year().month().day()) // "Apr 22, 2026" now.formatted(.dateTime.hour().minute()) // "4:30 PM" now.formatted(.dateTime.weekday(.wide).month(.wide).day()) // "Wednesday, April 22" // Predefined styles now.formatted(date: .long, time: .shortened) // "April 22, 2026 at 4:30 PM" now.formatted(date: .abbreviated, time: .omitted) // "Apr 22, 2026" // ISO 8601 now.formatted(.iso8601) // "2026-04-22T16:30:00Z" // Relative let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: .now)! yesterday.formatted(.relative(presentation: .named)) // "yesterday" yesterday.formatted(.relative(presentation: .numeric)) // "1 day ago" // Interval (date1.. String { switch value { case ..<1_000: return "\(value)" case 1_000..<1_000_000: return String(format: "%.1fK", Double(value) / 1_000) default: return String(format: "%.1fM", Double(value) / 1_000_000) } } } extension FormatStyle where Self == AbbreviatedCountStyle { static var abbreviatedCount: AbbreviatedCountStyle { .init() } } // Usage let followers = 12_500 Text(followers, format: .abbreviatedCount) // "12.5K" ``` ## Common Mistakes | Mistake | Fix | |---------|-----| | Using legacy `NumberFormatter` / `DateFormatter` in new code | Use `FormatStyle` (iOS 15+). Foundation caches format style instances automatically. | | String interpolation for formatted numbers in `Text` | Use `Text(value, format:)` for locale correctness and accessibility | | Hardcoding locale in format styles | Omit `.locale()` to inherit the user's current locale by default | | Using `.time(pattern:)` for labeled duration display | Use `.units(allowed:width:)` for "1 hr, 30 min" style output | | Creating `Formatter` instances in `body` or tight loops | FormatStyle instances are value types cached by Foundation; safe to create inline | | Formatting `Duration` with `DateComponentsFormatter` | Use `Duration.TimeFormatStyle` or `Duration.UnitsFormatStyle` directly | | Ignoring `usage:` parameter for measurements | Specify `.road`, `.asProvided`, etc. for locale-aware unit conversion | ## Review Checklist - [ ] `FormatStyle` used instead of legacy `Formatter` subclasses for iOS 15+ targets - [ ] `Text(_:format:)` used instead of pre-formatting strings for SwiftUI text - [ ] No hardcoded locale unless explicitly needed (e.g., server communication) - [ ] Duration formatting uses `Duration.TimeFormatStyle` or `Duration.UnitsFormatStyle` - [ ] Currency codes are ISO 4217 strings, not hardcoded symbols - [ ] Measurement formatting includes `usage:` for user-facing display - [ ] Custom FormatStyle types conform to `Codable` + `Hashable` for caching ## References - Apple docs: [FormatStyle](https://sosumi.ai/documentation/foundation/formatstyle) | [Date.FormatStyle](https://sosumi.ai/documentation/foundation/date/formatstyle) | [Duration.TimeFormatStyle](https://sosumi.ai/documentation/swift/duration/timeformatstyle)