toRadians() Method in Java

Rumman Ansari   Software Engineer   2019-03-30   6295 Share
☰ Table of Contents

Table of Content:


Description

This java tutorial shows an example on how to use the toRadians(double angdeg) method of Math class under java.lang package. This method returns the equivalent in radians the value specified angle in degrees as method argument.

The method converts the argument value to radians.

The toRadians(double angdeg) method is static thus we should invoke it statically for example Math.toRadians(double angdeg). This method simply returns the equivalent in radians the angle method argument. We would be using this widely since the trigonometric functions of Math class usually takes radians as an input which is very much different in real life applications since angles is usually represented in degrees.

Syntax

double toRadians(double d)

Parameters

Here is the detail of parameters ?

  • d ? A double data type. An angle, in degrees

Return Value

  • This method returns a double value.

Example

public class MathtoRadian {

   public static void main(String args[]) {
      double x = 45.0;
      double y = 30.0;

      System.out.println( Math.toRadians(x) );
      System.out.println( Math.toRadians(y) );
   }
}

Output

0.7853981633974483
0.5235987755982988
Press any key to continue . . .

Example

This java example source code demonstrates the use of toRadians method of Math class. Basically we just convert the angle taken from the user input into radians.


import static java.lang.System.*;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 * toRadians(double angdeg) method of Math class
 */

public class MathToRadians {

	public static void main(String[] args) {
		// ask for user input
		out.print("Enter angle in degrees:");
		Scanner scan = new Scanner(System.in);
		// use scanner to get user console input
		double degrees = scan.nextDouble();
		// convert the angle from degrees to radians
		double radians = Math.toRadians(degrees);
		out.println(degrees +" degress = "+radians +" radians");
		// close the scanner object to avoid memory leak
		scan.close();

	}

}

Output

Running the toRadians(double angdeg) method example source code of Math class will give you the following output.

Enter angle in degrees:90
90.0 degress = 1.5707963267948966 radians
Press any key to continue . . .

Example

The following example shows the usage of lang.Math.toRadians() method.


import java.lang.*;

public class MathDemo {

   public static void main(String[] args) {

      // get two double numbers numbers
      double x = 45;
      double y = -180;

      // convert them in radians
      x = Math.toRadians(x);
      y = Math.toRadians(y);

      // print the hyperbolic tangent of these doubles
      System.out.println("Math.tanh(" + x + ")=" + Math.tanh(x));
      System.out.println("Math.tanh(" + y + ")=" + Math.tanh(y));
   }
}

Output

Running the toRadians(double angdeg) method example source code of Math class will give you the following output.

Math.tanh(0.7853981633974483)=0.6557942026326724
Math.tanh(-3.141592653589793)=-0.99627207622075
Press any key to continue . . .