Javascript,奇怪的浮点数无穷小数? [英] Javascript, weird floating point number endless decimal?

查看:233
本文介绍了Javascript,奇怪的浮点数无穷小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是代码,如下所示:

 < script> 
$ b函数add()
{
document.getElementById(answer)。value = parseFloat(document.getElementById(num1).value)+ parseFloat(document。的getElementById( NUM2)值)。



$ b $ document.getElementById(answer)。value = parseFloat(document.getElementById(num1)。value) - parseFloat(document.getElementById(num2).value);


函数multiply()
{
document.getElementById(answer).value = parseFloat(document.getElementById(num1).value) * parseFloat(document.getElementById(num2).value);


函数divide()
{
document.getElementById(answer).value = parseFloat(document.getElementById(num1).value) / parseFloat(document.getElementById(num2).value);
}

< / script>

抱歉有点长!然后html很简单:

 < h1>计算器< / h1> 

< h3>在下面的每个框中输入一个值,然后点击一个操作< / h3>

< form id =calculatorForm>
< input id =num1value =0type =text>
< input id =num2value =0type =text>
< br>
< input value =+onclick =add();类型= 按钮 >
< input value = - onclick =subtract();类型= 按钮 >
< input value =*onclick =multiply();类型= 按钮 >
< input value =/onclick =divide();类型= 按钮 >
< br>
< input id =answervalue =0type =text>
< / form>

你几乎可以猜到我的问题会是什么:当我乘,除或减二浮点数,我结束了一个无限小数。

我需要一个快速的解决方案,将这些数字四舍五入到两个小数点,我需要它以后工作,因为之后我需要执行华氏转Celsisus操作。



我不在乎这是怎么做的,但是必须成为Javascript。对不起,如果这之前已经回答,但我很快就需要一个答案!谢谢!



编辑:对有帮助的人回答我的问题,谢谢!

解决方案使用 .toFixed()

  var num = 45.34343434343; 

num = num.toFixed(2); //45.34


I need some help with floating point numbers...please!

Here's the thing, I have code like below:

<script>

    function add()
    {
        document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) + parseFloat(document.getElementById("num2").value);
    }

    function subtract()
    {
        document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) - parseFloat(document.getElementById("num2").value);
    }

    function multiply()
    {
        document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) * parseFloat(document.getElementById("num2").value);
    }

    function divide()
    {
        document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) / parseFloat(document.getElementById("num2").value);
    }

</script>

Sorry it's a bit long! And then the html is pretty simple:

<h1>Calculator</h1>

<h3>Enter a value in each box below, then click an operation</h3>

<form id="calculatorForm">
    <input id="num1" value="0" type="text">
    <input id="num2" value="0" type="text">
    <br>
    <input value="+" onclick="add();" type="button">
    <input value="-" onclick="subtract();" type="button">
    <input value="*" onclick="multiply();" type="button">
    <input value="/" onclick="divide();" type="button">
    <br>
    <input id="answer" value="0" type="text">
</form>

You can pretty much guess what my question is gonna be: when I multiply, divide, or subtract two floating point numbers, I end up with an infinite decimal.

I need a quick solution that will round those numbers to two decimal points, and I need it to work later on, because I then need to implement Fahrenheit-to-Celsisus operations afterwards.

I don't care how this is done, but it must be Javascript. Sorry if this has been answered before, but I really need an answer soon! Thanks!

EDIT: A BIG Thankyou to the helpful people who answered my questions. Thank you!

解决方案

Use .toFixed():

var num = 45.34343434343;

num = num.toFixed(2); // "45.34"

这篇关于Javascript,奇怪的浮点数无穷小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆