Swift Temperature Format Customization

When you use Measurement format function in Swift it will always format the temperature to the format of the locale of the device. This is not the experience I wanted in my application. I wanted to provide the user an option to chose which scale they wanted to use, so I had to override the format operation. I used the following function to make this happen.

func formatTemperature(_ temperature: Double, _ scale: UnitTemperature) -> String {
	let source = Measurement(value:temperature, unit: UnitTemperature.celsius)
	let converted = source.converted(to: scale)
	return "\(String(format: "%.1f", converted.value))\(converted.unit.symbol)"
}