Measurement.FormatStyle
A type that provides localized representations of measurements.
Declaration
struct FormatStyleOverview
A measurement format style creates human-readable text from a Measurement. You can customize the formatting behavior of the format style using the width, numberFormat, usage, and locale properties. The system automatically caches unique configurations of Measurement.FormatStyle to enhance performance.
Use either the formatted() or the formatted(_:) instance method of Measurement to create a string representation of a measurement.
The formatted() method generates a string using the default measurement format style.
let temperature = Measurement<UnitTemperature>(value: 38, unit: .celsius)
temperature.formatted()
// For locale: en_US: 100°F
// For locale: fr_FR: 38°CThe default format style in the previous example abbreviates the measurement unit. To customize any of the properties of the formatted measurement, you provide a measurement format style to the formatted(_:) method. For example, to create a string with the full name of the unit, the code might resemble the following:
temperature.formatted(.measurement(width: .wide))
// For locale: en_US: 100 degrees Fahrenheit
// For locale: fr_FR: 38 degrés CelsiusThe previous example uses a static factory method to create a measurement format style within the call to the formatted(_:) method. You can also create a measurement format style and pass it to the method, such as in the following example:
let distance = Measurement<UnitLength>(value: 36, unit: .miles)
let distanceStyle = Measurement<UnitLength>.FormatStyle(width: .wide, usage: .road)
distanceStyle.format(distance)
// for locale: en_US: 36 miles
// for locale: fr_FR: 58 kilomètres
After you create an instance of a format style, you can use it to format measurements of the same unit type.
Topics
Creating a measurement format style
init(width:locale:usage:numberFormatStyle:)init(width:locale:usage:hidesScaleName:numberFormatStyle:)