EN
TypeScript - convert radians to degrees (rad to deg)
0 points
In this article, we're going to have a look at how to convert radians to degrees using TypeScript. In TypeScript, there is no built-in conversion method. It is necessary to write your own one.
Conversion can be made with the following formula:
A simple implementation is presented below.
xxxxxxxxxx
1
const radToDeg = (rad: number): number => {
2
return rad * (180.0 / Math.PI);
3
};
4
5
// Usage example:
6
console.log(radToDeg(0)); // 0.0 degrees
7
console.log(radToDeg(Math.PI / 2)); // 90.0 degrees
8
console.log(radToDeg(Math.PI)); // 180.0 degrees
9
10
console.log(radToDeg(3.141592653589793 / 6)); // ~30 degrees
11
console.log(radToDeg(3.141592653589793 / 4)); // 45 degrees
12
console.log(radToDeg(3.141592653589793 / 2)); // 90 degrees
13
console.log(radToDeg(3.141592653589793)); // 180 degrees
14
console.log(radToDeg(3.141592653589793 * 1.5)); // 270 degrees
15
console.log(radToDeg(3.141592653589793 * 2)); // 360 degrees
Output:
xxxxxxxxxx
1
0
2
90
3
180
4
5
29.999999999999996
6
45
7
90
8
180
9
270
10
360