30秒学会 JavaScript 片段 · 2023年9月3日

30秒学会 JavaScript 片段 – Convert between degrees and radians in JavaScript

Degrees to radians

JavaScript’s Math.PI constant can be used to convert an angle from degrees to radians. The conversion formula is radians = degrees * Math.PI / 180.0.

代码实现

const degreesToRads = deg => (deg * Math.PI) / 180.0;

degreesToRads(90.0); // ~1.5708

Radians to degrees

Conversely, the conversion formula from radians to degrees is degrees = radians * 180.0 / Math.PI.

使用样例

const radsToDegrees = rad => (rad * 180.0) / Math.PI;

radsToDegrees(Math.PI / 2); // 90

翻译自:https://www.30secondsofcode.org/js/s/convert-degrees-radians