How to convert Radians to Degrees in Swift

⋅ 2 min read ⋅ Swift

Table of Contents

In this article, we will learn different ways to convert Radians to Degrees in iOS.

What is Radian

There are two different units used to measure an angle

  1. Radians
  2. Degrees

Radian is a widely used angle unit in Physics and engineering, making it a unit of choice in most APIs that accepts an angle as a parameter.

Here is an example of an API to create a rotation transform. The API accepts an angle in radians.

let view = UIView()
view.transform = CGAffineTransform(rotationAngle: .pi)

While Radian is used for calculation, Degrees is more popular and easier to understand for most users.

So, you might want to convert between the two. And that's what we will learn in this article.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

How to convert Radians to Degrees in iOS

There are many ways to convert Radians to Degrees. I will show you different ways to do it in iOS.

  1. Create a function
  2. Create an extension
  3. Use the Angle type.
  4. Use the Measurement struct

Function to convert Radians to Degrees in iOS

We can easily convert between degrees and radians using simple math.

  • A complete rotation angle in radians unit is 2π Radians.
  • A complete rotation angle in degrees unit is 360 Degrees.

So, 1 Radian = 360 Degrees/2π = 180 Degrees/π

We can create a Swift helper function like this:

func radiansToDegrees(_ radians: Double) -> Double {
return radians * 180 / .pi
}

Extension to convert Radians to Degrees in iOS

Instead of a function, you can create an extension for this.

extension Double {
var degreesUnit: Double {
return self * 180 / .pi
}
}

Convert Radians to Degrees using SwiftUI

SwiftUI has a dedicated Angle type.

Angle type allows us to:

  • Initialize by either Degress and Radians.
  • Getting a value in either Degress and Radians unit.

If you don't mind importing the SwiftUI module, you can enjoy this nice struct.

import SwiftUI

let angleInRadians = Angle(radians: .pi)
let angleInDegrees = angleInRadians.degrees

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Convert Radians to Degrees using Measurement structure

Apple got a Measurement struct which is a struct used to describe a unit of measure.

It supports a wide range of units, and angle (UnitAngle) is one of them.

The Measurement struct has built-in support for calculation and conversion.

You can use it by initializing it with your desired value and unit. Then, you can convert them using a built-in method.

In this case, we convert radians to degrees.

let radians = Measurement(value: .pi, unit: UnitAngle.radians)
let degrees = radians.converted(to: .degrees)

Read more article about Swift or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
Default Value in Swift Dictionary

Learn how to set and get value from a dictionary with a default value.

Next
Better print for AnyKeyPath in Swift 5.8

Swift 5.8 improve the information that is printed out for a keypath.

← Home