Contents

formatted(_:)

Generates a locale-aware string representation of a measurement using the provided measurement format style.

Declaration

func formatted<S>(_ style: S) -> S.FormatOutput where S : FormatStyle, S.FormatInput == Measurement<UnitType>

Parameters

  • style:

    The measurement format style to apply to the measurement.

Return Value

A string, formatted according to the provided style.

Discussion

Use the formatted(_:) method to create a string representation of a measurement using a custom measurement format style. You can specify the width of the unit name, the numeric formatting of the value, and the intended usage type of the measurement. You can use the Measurement.FormatStyle static factory method measurement(width:usage:numberFormat:) to create a custom format style as a parameter to the method, such as in the following example:

let temp = Measurement<UnitTemperature>(value: 38, unit: .celsius)
let formattedTemp = temp.formatted(.measurement(width: .wide, usage: .weather, numberFormat: .numeric(precision: .fractionLength(1))))
// For locale: en_US: 100.4 degrees Fahrenheit

See Also

Formatting a Measurement