Contents

locale(_:)

Modifies the format style to use the specified locale.

Declaration

func locale(_ locale: Locale) -> Decimal.FormatStyle

Parameters

  • locale:

    The locale to apply to the format style.

Return Value

A decimal 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 that this format style uses, use the locale property.

The following example creates a default Decimal.FormatStyle 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, but that uses the German (DE) locale. It then applies each style to an array of decimal values.

let compactStyle = Decimal.FormatStyle(locale: Locale(identifier: "en_US"))
    .notation(.compactName)
let germanStyle = compactStyle.locale(Locale(identifier: "DE"))
let nums: [Decimal] = [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."]

See Also

Customizing style behavior