Java toRadians Function

The Java toRadians Function is one of the Math Library functions, which is to converts an angle measured in degrees to an approximately equivalent angle measured in radians. In this article, we will show how to use Java Math.toRadians function with example, and the syntax of it in this Programming language is

static double toRadians(double number); //Return Type is Double

// To use in program: 
Math.toRadians(double number);

Number: It can be an angle in degree for which you want to convert as a radian

Java toRadians Function Example

The Java Math.toRadians Function allows to converts degrees to an approximately equivalent angle measured in radians. In this program, We are going to find the radians of both positive and negative values and display the output

package TrigonometricFunctions;

public class ToRadians {
	public static void main(String[] args) {
		double x = Math.toRadians(400.96 + 240.65 - 106.98);
		System.out.println("toRadians Function result =  " + x);	
		
		System.out.println("\nFinding Radians of Zero = " + Math.toRadians(0));
		
		System.out.println("\nFinding Radians of Positive Number = " + Math.toRadians(104.95));
		System.out.println("Finding Radians of Positive Number=  " + Math.toRadians(156.28));
		
		System.out.println("\nFinding Radians of Negative Number =  " + Math.toRadians(-135.85));
		System.out.println("Finding Radians of Negative Number =  " + Math.toRadians(-9.35));
	}
}
Java toRadians Function 1

First, We declared variable x of type Double and used the Java Math.toRadians function directly on expression. Here, we used System.out.println statement to print the radiant result as output.

double x = Math.toRadians(400.96 + 240.65 - 106.98);
System.out.println("toRadians Function result =  " + x);

Next, We used the Math.toRadians Function directly on Positive double values.

System.out.println("\nFinding Radians of Positive Number = " + Math.toRadians(104.95));
System.out.println("Finding Radians of Positive Number=  " + Math.toRadians(156.28));

Next, We used the toRadians Math function directly on Negative double values.

System.out.println("\nFinding Radians of Negative Number =  " + Math.toRadians(-135.85));
System.out.println("Finding Radians of Negative Number =  " + Math.toRadians(-9.35));

Java toRadians on Array example

In this Java program, we convert the bulk data to radians. Here, we are going to declare an Array of double type. Next, we use toRadians function to convert the array elements to radians.

package TrigonometricFunctions;

public class ToRadiansOnArrays {
	public static void main(String[] args) {
		
		double [] myArray = {1, 30, 45, 60, 75, 90, 120, 180, 240, 360};

		for (int i = 0; i < myArray.length; i++) {
			System.out.format("Finding Radians of Array Element %.2f is = %.4f\n", myArray[i], Math.toRadians(myArray[i]));
		}
	}
}
Finding Radians of Array Element 1.00 is = 0.0175
Finding Radians of Array Element 30.00 is = 0.5236
Finding Radians of Array Element 45.00 is = 0.7854
Finding Radians of Array Element 60.00 is = 1.0472
Finding Radians of Array Element 75.00 is = 1.3090
Finding Radians of Array Element 90.00 is = 1.5708
Finding Radians of Array Element 120.00 is = 2.0944
Finding Radians of Array Element 180.00 is = 3.1416
Finding Radians of Array Element 240.00 is = 4.1888
Finding Radians of Array Element 360.00 is = 6.2832

We used For Loop to iterate the Array. Within the toradians For Loop, we initialized the i value as 0. Next, the compiler will check the condition (i < myArray.length).

TIP: myArray.length finds the length of an array.

for (int i = 0; i < myArray.length; i++) {

Here, we used the Math.toRadians Function directly inside the System.out.format statement. It means the compiler will call the Math.toRadians method ( static double toRadians(double number) ) to find the corresponding radiant values.

System.out.format("Finding Radians of Array Element %.2f is = %.4f\n", myArray[i], Math.toRadians(myArray[i]));

NOTE: To find the radiant value of a single item, then use: Math.toRadians(myArray[index_position])

Java toRadians Function on Arraylist example

In this Java program, we are going to declare an Arraylist of double type and find the Radiant values of list elements.

package TrigonometricFunctions;

import java.util.ArrayList;

public class ToRadiansOnArrayList {
	public static void main(String[] args) {
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(Math.PI);
		myList.add(Math.PI / 3);
		myList.add(Math.PI / 4);
		myList.add(Math.PI / 6);
		myList.add(Math.PI / 9);
		
		for (double x : myList) {
			System.out.format("Finding Radians of ArrayList Item %.2f =  %.4f \n", x, Math.toRadians(x));
		}
	}
}
Finding Radians of ArrayList Item 3.14 =  0.0548 
Finding Radians of ArrayList Item 1.05 =  0.0183 
Finding Radians of ArrayList Item 0.79 =  0.0137 
Finding Radians of ArrayList Item 0.52 =  0.0091 
Finding Radians of ArrayList Item 0.35 =  0.0061 

Within this toRadians function example, We declared an ArrayList of double type and assigned random values using Math.PI constant (its value is approximately 3.14).

ArrayList<Double> myList = new ArrayList<Double>(5);
myList.add(Math.PI);
myList.add(Math.PI / 3);
myList.add(Math.PI / 4);
myList.add(Math.PI / 6);
myList.add(Math.PI / 9);

Next, We used the Java For Loop to iterate the double values in ArrayList

for (double x : myList) {

Here, the compiler will call the math.toRadians method ( static double toRadians(double x) ) to find the corresponding radiant value, and prints the output.

System.out.format("Finding Radians of ArrayList Item %.2f =  %.4f \n", x, Math.toRadians(x));