locale(_:)
Modifies the format style to use the specified locale.
Declaration
func locale(_ locale: Locale) -> FloatingPointFormatStyle<Value>Parameters
- locale:
The locale to apply to the format style.
Return Value
A floating-point format style modified to use the provided locale.
Discussion
Use this modifier to change the locale that an existing format style uses. To instead determine the locale this format style uses, use the locale property.
The following example creates a default FloatingPointFormatStyle for the en_US locale, and applies the notation(_:) modifier to use compact name notation. Next, the sample creates a second style based on this first style, but using the German (DE) locale. It then applies each style to an array of floating-point values.
let compactStyle = FloatingPointFormatStyle<Double>(locale: Locale(identifier: "en_US"))
.notation(.compactName)
let germanStyle = compactStyle.locale(Locale(identifier: "DE"))
let nums: [Double] = [100, 1000, 10000, 100000, 1000000]
let enUSCompactNums = nums.map { compactStyle.format($0) } // ["100", "1K", "10K", "100K", "1M"]
let deCompactNums = nums.map { germanStyle.format($0) } // ["100", "1000", "10.000", "100.000", "1 Mio."]