迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中将弧度转换为度数

作者:迹忆客 最近更新:2023/09/09 浏览次数:

在本文中,我们将学习 JavaScript 源代码中的数学计算,使用公式从弧度值获取角度值,并使用默认的警报框或日志框将结果显示给用户。


在 JavaScript 中将弧度转换为度数

在 JavaScript 中,我们可以使用算术运算符(+-*/)执行算术运算和计算,例如加法、减法、乘法和除法。 借助这些运算符,我们可以在 JavaScript 程序中创建任何数学公式。

为了在 JavaScript 中将弧度转换为度数,我们可以使用以下公式计算并找出度数值:

radian value * (half circle angle/value of pi )

在 JavaScript 中,pi 值有一个默认的 Math.PI 关键字,它返回 3.14。 这个默认值在我们的弧度到度数转换公式中非常有用且高效,而不是编写 3.14。

基本公式:

let radian = 1
let degree = radian * (180 / Math.PI);

如上所示,我们只需要用户预先定义的弧度值即可确定角度值。 我们可以得到默认的 Math.PI 值,即 3.14。

例子:

在下面的示例中,我们将从用户输入中获取弧度值,并使用弧度转角度公式进行算术运算。 我们将在警报框中向用户显示结果。

<html>
    <head>
        <title> convert radian to degree </title>
    </head>
    <script>
    function convertToDegree()
    {
        let data = document.getElementById("value").value;
        data = parseInt(data)

        if(isNaN(data)) //check empty data field
                  {
                 alert("Please enter value");
                  }
      else{
      let degree = data * (180 / Math.PI); //Math.PI = 3.14
      degree = degree.toFixed(5)
       alert("Converted value into degree : "+degree+"°");
       }
    }
    </script>
    <body>
        <h1 style="color:blueviolet">Jiyik Learning</h1>
        <h3>JavaScript Convert radian to degree</h3>

        <form onsubmit ="convertToDegree()">
            <!-- data input -->

            <td> Enter Radian ualue: </td>
            <input id = "value" type="number">

            <br><br>
            <input type = "submit" value = "Convert">
        </form>
    </body>
</html>

我们使用上面 HTML 源代码中的 form 元素来创建用户输入字段和提交按钮。 用户将插入弧度值,当用户单击提交按钮时,将触发函数 convertToDegree()

在脚本标签中,我们声明了 convertToDegree() 函数。 在该函数内,我们在数据变量中获取用户输入值,并使用条件语句 if 来检查输入值。

如果变量为空或不包含任何值,它将显示 alert() 函数并显示错误消息。

如果用户提供了该值,我们将使用弧度到度数转换公式在 else 条件下执行算术运算,并将结果存储在 Degree 变量中。 我们将浮点值四舍五入为 5 位,并在alert() 弹出窗口中显示结果。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便