In this post, I’ll share how to display the temperature with unit using the MeasurementFormatter.
MeasurementFormatter
let numFormatter = NumberFormatter()numFormatter.maximumFractionDigits = 0let measureFormatter = MeasurementFormatter()measureFormatter.numberFormatter = numFormatterlet kelvin: Double = 294var temperature: String = ""let kelvinTemperature = Measurement( value: kelvin, unit: UnitTemperature.kelvin)//temperature is automatically changed the celcius / farenheit depending on locale.temperature = measureFormatter.string(from: kelvinTemperature)//70°Fprint(temperature)
UnitStyle
//70 degrees FahrenheitmeasureFormatter.unitStyle = .long
//70°FmeasureFormatter.unitStyle = .medium
//70°measureFormatter.unitStyle = .short
The default unitStyle is medium. You can set the three different unitStyle.
Apple Weather App
The unitStyle is short.

