Sling Academy
Home/Kotlin/Converting Between Degrees and Radians in Kotlin

Converting Between Degrees and Radians in Kotlin

Last updated: November 30, 2024

In programming, we often need to convert between degrees and radians, especially when dealing with geometric computations or graphics rendering. Here, we will explore how to perform these conversions using Kotlin, a JVM-based language that's concise and expressive.

Understanding the Concepts

Before we dive into Kotlin code, it's important to understand the mathematical relationship between degrees and radians:

  • Degrees are the most common way of measuring angles. One full rotation around a circle is 360 degrees.
  • Radians are another unit to measure angles, based on the radius of a circle. One full rotation around a circle is 2π radians.

The conversion formulas are:

  • To convert degrees to radians: radians = degrees * π / 180
  • To convert radians to degrees: degrees = radians * 180 / π

Converting Degrees to Radians in Kotlin

Here's a simple function in Kotlin that converts degrees to radians:

fun degreesToRadians(degrees: Double): Double {
    return degrees * Math.PI / 180
}

Let's use this function within a main function to see an example:

fun main() {
    val degrees = 180.0
    val radians = degreesToRadians(degrees)
    println("$degrees degrees is equal to $radians radians")
}

When you run the above main function, it will output:

180.0 degrees is equal to 3.141592653589793 radians

Converting Radians to Degrees in Kotlin

Similarly, we can create a function to convert radians to degrees:

fun radiansToDegrees(radians: Double): Double {
    return radians * 180 / Math.PI
}

And here's an example usage of this conversion function:

fun main() {
    val radians = Math.PI
    val degrees = radiansToDegrees(radians)
    println("$radians radians is equal to $degrees degrees")
}

Upon execution, the above code will print:

3.141592653589793 radians is equal to 180.0 degrees

Key Takeaways

  • Conversions between degrees and radians are pivotal in many fields such as graphics, physics simulations, and mathematical calculations.
  • Remember the conversion formulas, as they are quite straightforward and will serve you well across various languages and platforms.
  • Kotlin's Math.PI offers a precise representation of π, ensuring accuracy in these calculations.

With these simple functions, you can efficiently perform necessary angle conversions in your Kotlin applications.

Next Article: Rounding Numbers in Kotlin: Floor, Ceil, and Round Functions

Previous Article: Using Trigonometric Functions in Kotlin: `sin`, `cos`, `tan`

Series: Primitive data types in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin