Converting Angles with toDegrees() Method in Java: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-04 04:18:44   6151  Share
Subject Syllabus DetailsSubject Details 1 Program
☰ TContent
☰Fullscreen

Table of Content:

Description

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

The method converts the argument value to degrees.

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

Syntax

double toDegrees(double d)

Parameters

Here is the detail of parameters ?

  • d ? A double data type.

Return Value

  • This method returns a double value.

Example

public class MathToDegreeMethod {

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

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

Output

2578.3100780887044
1718.8733853924698
Press any key to continue . . .

Example

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

 
import static java.lang.System.*;

import java.util.Scanner;

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

public class MathToDegrees {

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

	}

}

Output

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

Enter angle in radians:5.6
5.6 radians = 320.85636527326096 degrees
Press any key to continue . . .