Java Math.toDegrees() method converts the angle given in radians to degrees. This is the approximate conversion and result are not exactly what you would expect.
public class JavaExample
{
public static void main(String[] args)
{
double radians = Math.PI;
// radians to degrees conversion
double degrees = Math.toDegrees(radians);
System.out.println(degrees);
}
}
Output:
180.0
Syntax of Math.toDegrees() method
Math.toDegrees(Math.PI/90); //returns 2.0
toDegrees() Description
public static double toDegrees(double radians): It returns the approximate angle value in degrees after the conversion.
toDegrees() Parameters
- radians: A double value that represents angle in radians.
toDegrees() Return Value
- The measurement of given argument
radiansin degrees. - If the given argument is NaN (Not a number), then it returns NaN.
- If the given argument is zero, then it returns zero with the same sign.
- If the given argument is infinite, then it returns the infinity with the same sign.
Example 1: Radians to Degrees Conversion
public class JavaExample
{
public static void main(String[] args)
{
double r1 = Math.PI;
double r2 = Math.PI/4;
double r3 = Math.PI/10;
System.out.println(Math.toDegrees(r1));
System.out.println(Math.toDegrees(r2));
System.out.println(Math.toDegrees(r3));
}
}
Output:

Example 2: Zero, NaN and Infinity to Degrees
public class JavaExample
{
public static void main(String[] args)
{
double r1 = 0;
double r2 = 0.0/0; //NaN
double r3 = Double.POSITIVE_INFINITY; //infinity
System.out.println(Math.toDegrees(r1));
System.out.println(Math.toDegrees(r2));
System.out.println(Math.toDegrees(r3));
}
}
Output:

Example 3: Double max and min values to degrees
public class JavaExample
{
public static void main(String[] args)
{
double r1 = Double.MAX_VALUE;
double r2 = Double.MIN_VALUE;
System.out.println(Math.toDegrees(r1));
System.out.println(Math.toDegrees(r2));
}
}
Output:
