Convert Celsius to Fahrenheit and back in Swift

Photo by Ilse Orsel on Unsplash

Convert Celsius to Fahrenheit and back in Swift

Conversions between temperature scales are needed, especially in a world where three countries (the United States, the Cayman Islands and Liberia) use Fahrenheit in contrast to the Celsius scale.

Thankfully you don't have to remember the conversion formulae as an iOS developer. The Foundation framework comes to the rescue with its Measurement type.

A programmatic interface to convert measurements into different units, as well as calculate the sum or difference between two measurements.

The Foundation framework provides concrete Unit subclasses for many of the most common types of physical units. This includes temperature scales :)

let celsius = Measurement(value: 20.0, unit: UnitTemperature.celsius)
let fahrenheit = celsius.converted(to: .fahrenheit).value
let kelvin = celsius.converted(to: .kelvin).value
print(fahrenheit) // 67.99999999999571
print(c.converted(to: .kelvin).value)     // "293.15"

Use the MeasurementFormatter to present the conversion result to the end user.

A formatter that provides localized representations of units and measurements.

let formatter = MeasurementFormatter()
formatter.unitOptions = .providedUnit

let t = Measurement(value: 20.0, unit: UnitTemperature.celsius)
print(formatter.string(from: t)) //20°C
print(formatter.string(from: t.converted(to: .fahrenheit))) // 68°F
print(formatter.string(from: t.converted(to: .kelvin))) // 293.15 K

Let me finish my post with the following advice: Check the Unit subclasses below before your try implementing your own unit conversion 😊

Unit subclassDescriptionBase unit
UnitAccelerationUnit of measure for accelerationmeters per second squared (m/s²)
UnitAngleUnit of measure for planar angle and rotationdegrees (°)
UnitAreaUnit of measure for areasquare meters (m²)
UnitConcentrationMassUnit of measure for concentration of massgrams per liter (g/L)
UnitDispersionUnit of measure for dispersionparts per million (ppm)
UnitDurationUnit of measure for duration of timeseconds (sec)
UnitElectricChargeUnit of measure for electric chargecoulombs (C)
UnitElectricCurrentUnit of measure for electric currentamperes (A)
UnitElectricPotentialDifferenceUnit of measure for electric potential differencevolts (V)
UnitElectricResistanceUnit of measure for electric resistanceohms (Ω)
UnitEnergyUnit of measure for energyjoules (J)
UnitFrequencyUnit of measure for frequencyhertz (Hz)
UnitFuelEfficiencyUnit of measure for fuel efficiencyliters per 100 kilometers (L/100km)
UnitIlluminanceUnit of measure for illuminancelux (lx)
UnitInformationStorageUnit of measure for quantities of informationbytes (b)
UnitLengthUnit of measure for lengthmeters (m)
UnitMassUnit of measure for masskilograms (kg)
UnitPowerUnit of measure for powerwatts (W)
UnitPressureUnit of measure for pressurenewtons per square meter (N/m²)
UnitSpeedUnit of measure for speedmeters per second (m/s)
UnitTemperatureUnit of measure for temperaturekelvin (K)
UnitVolumeUnit of measure for volumeliters (L)

Did you find this article valuable?

Support Marco Eidinger by becoming a sponsor. Any amount is appreciated!