Follow

# Swifty Tech by Marco Eidinger

Follow  Photo by Ilse Orsel on Unsplash

# Convert Celsius to Fahrenheit and back in Swift

Marco Eidinger
·Oct 28, 2022·

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` subclass Description Base unit UnitAcceleration Unit of measure for acceleration meters per second squared (m/s²) UnitAngle Unit of measure for planar angle and rotation degrees (°) UnitArea Unit of measure for area square meters (m²) UnitConcentrationMass Unit of measure for concentration of mass grams per liter (g/L) UnitDispersion Unit of measure for dispersion parts per million (ppm) UnitDuration Unit of measure for duration of time seconds (sec) UnitElectricCharge Unit of measure for electric charge coulombs (C) UnitElectricCurrent Unit of measure for electric current amperes (A) UnitElectricPotentialDifference Unit of measure for electric potential difference volts (V) UnitElectricResistance Unit of measure for electric resistance ohms (Ω) UnitEnergy Unit of measure for energy joules (J) UnitFrequency Unit of measure for frequency hertz (Hz) UnitFuelEfficiency Unit of measure for fuel efficiency liters per 100 kilometers (L/100km) UnitIlluminance Unit of measure for illuminance lux (lx) UnitInformationStorage Unit of measure for quantities of information bytes (b) UnitLength Unit of measure for length meters (m) UnitMass Unit of measure for mass kilograms (kg) UnitPower Unit of measure for power watts (W) UnitPressure Unit of measure for pressure newtons per square meter (N/m²) UnitSpeed Unit of measure for speed meters per second (m/s) UnitTemperature Unit of measure for temperature kelvin (K) UnitVolume Unit of measure for volume liters (L)