In Java, Math.toDegrees(angrad) converts an angle from radians to degrees. Half a turn is Math.PI radians, so Math.toDegrees(Math.PI) is 180.0.
Most trigonometry methods work in radians, which is hard to read. Use toDegrees to show the answer of asin, acos, atan, or atan2 to a person. It has been part of java.lang.Math since the early versions of Java.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.toDegrees(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass an angle in radians as a double. The same angle in degrees comes back.
The direction of (x, y) in degrees, from -180 to 180.
There is only a double version. The method does not wrap angles into 0 to 360: Math.toDegrees(7.0) is about 401.07. Normalise afterwards with ((deg % 360) + 360) % 360.
Library declarations
Java
// java.lang.Math
public static double toDegrees(double angrad)
Parameters
Parameter
Description
angrad
An angle measured in radians.
Return Value
Return Value
Description
double
The same angle in degrees. NaN and infinities stay NaN or infinite, and the sign of a zero is kept.
Fractions of PI. A quarter, half, and whole PI map to 45, 90, and 180 degrees; each is a power-of-two fraction of PI, so the result comes out clean under every formula that Java versions use.
Sign and size. A full turn is 360.0 and a negative angle stays negative (-180.0), because the conversion is only a multiplication.
One radian. The number of degrees per radian is 180 / PI, about 57.29577951308232; this is the factor that the conversion applies to every input.
Example 2 — Why the Result Can Be 59.99999999999999
Some angles do not convert to a "round" number of degrees. Round them for display and compare with a tolerance.
PI / 3 rounded: 60.00
PI / 6 rounded: 30.00
PI / 3 close to 60? true
round trip: 1.2345
round trip close? true
How It Works
PI / 3 is not exact.Math.PI / 3 is a rounded double, so the result may print as 59.99999999999999 or as 60.0 depending on the JDK; %.2f gives 60.00 in both cases.
A tolerance test is portable. Whatever the last digit is, the distance from 60 is around 1E-14, so Math.abs(sixty - 60) < 1e-9 is true on every Java version.
Round trip. Converting 1.2345 radians to degrees and back returns the same angle to well within 1e-12, so the pair of methods undo each other, up to a tiny rounding error.
Example 3 — Sensor Angles in Radians, Shown as Degrees and as a Dial Reading
A robot arm reports joint angles in radians. Convert them for the screen, and bring an angle that has gone past a full turn back to 0-360.
Java
import java.util.Locale;
public class MathToDegreesDial {
public static void main(String[] args) {
double[] joints = {0.5, 1.0, Math.PI / 2, 3.0};
for (double rad : joints) {
System.out.println(rad + " rad = " + String.format(Locale.US, "%.2f", Math.toDegrees(rad)) + " degrees");
}
double[] spins = {7.0, -1.0};
for (double rad : spins) {
double deg = Math.toDegrees(rad);
double dial = ((deg % 360) + 360) % 360;
System.out.println(rad + " rad = " + String.format(Locale.US, "%.2f", deg)
+ " degrees = " + String.format(Locale.US, "%.2f", dial) + " on the dial");
}
}
}
Output
0.5 rad = 28.65 degrees
1.0 rad = 57.30 degrees
1.5707963267948966 rad = 90.00 degrees
3.0 rad = 171.89 degrees
7.0 rad = 401.07 degrees = 41.07 on the dial
-1.0 rad = -57.30 degrees = 302.70 on the dial
How It Works
About 57.3 per radian. Half a radian is 28.65 degrees and one radian is 57.30; the quarter turn Math.PI / 2 is 90.00, and 3.0 radians is almost a half turn.
Beyond a full turn. 7.0 radians is 401.07 degrees, which is one full turn (360) plus 41.07, so % 360 leaves 41.07 on the dial.
Negative angles need a second step. In Java -57.30 % 360 stays -57.30, so adding 360 and applying % 360 again gives 302.70, the positive angle for the same direction.
Frequently Asked Questions
It converts an angle from radians to degrees. Math.toDegrees(Math.PI) returns 180.0 and Math.toDegrees(Math.PI / 2) returns 90.0.
Math.PI / 3 is a rounded double, and the multiplication by 180 / PI rounds again. Depending on the JDK version the result is 59.99999999999999 or 60.0. Round for display or compare with a tolerance.
Wrap it: Math.toDegrees(Math.atan2(y, x)). The result runs from -180 to 180; use (deg + 360) % 360 to get 0 to 360.
Mathematically radians * 180 / PI. Older JDKs compute angrad * 180.0 / PI and newer ones multiply by a constant, so some results differ in the last digit.
Use Math.toRadians. Math.toRadians(180) returns Math.PI. It is the reverse of toDegrees.