数学甚至比javascript更难

时间:2013-12-12 23:47:14

标签: javascript math numbers

所以我在找到“// var”-s的部分有问题。基本上我需要它来显示如果我吃特定数量的特定汉堡将获得多少千克和克。例如汉堡是260卡路里= 0.260千克。 0,260 * 4份= 1公斤40克。但它没有显示出来。显示真正的圆形数字。真的需要帮助,已经坐了3个多小时试图弄清楚什么是错的..

<!doctype html>
<html>
    <head>
    <style type="text/css">
    body{font-size:150%;}
    </style>
        <title>FAT CALCULATOR 4000</title>
        <meta charset="utf-8" />
            <script>
             var calories=new Array(0, 260, 310, 460, 560, 400, 370);
             var cals=calories[0];

            function calsMuutus(){
                    cals=calories[document.getElementById("hamburger").selectedIndex];
                    arvuta();
                }
            function arvuta(){
                var kogus1=document.getElementById("kogus").value;
                if(isNaN(kogus1)){
                    document.getElementById("kaal").value="...";
                    return;
                    }


                //var kaal=calsg*kogus;
                var calsg=(cals/1000);  
                //var kogusg=kogus1/1000;
                var kaal=(calsg*kogus1);
                var kaalk=parseInt(kaal);
                var kaalg=parseInt(kaal)%1000;
                var kaal=kaalk+" kg. "+kaalg+" gr.";

                document.getElementById("kaal").value=kaal;
                }
            function arvuta2(){
                var kaal1=document.getElementById("kaal").value;
                if(isNaN(kaal1)){
                    document.getElementById("kogus").value="...";
                    return;
                    }
                var kogus=(kaal1/calsg);
                document.getElementById("kogus").value=kogus;
                }

            </script>
    </head>
    <body background="calc_bg.jpg" >
    <center>

        <div>
        <table>
        <tr>
        <td>
        <h1>FAT CALCULATOR 4000</h1>
            Choose hamburger<br />
            <select id="hamburger" onchange="calsMuutus()" >
                <option> </option>
                <option>Hamburger</option>
                <option>Cheeseburger</option>
                <option>Double Cheeseburger</option>
                <option>Big Mac</option>
                <option>Filet-O-Fish</option>
                <option>McChicken</option>

            </select><br />
            <p></p>
            Amount: <br />
            <input type="text" id="kogus" onkeyup="arvuta()" /><br />
            <p></p>
            You gain:<br />
            <input type="text" id="kaal" onkeyup="arvuta2()"  /><br />
        </tr>
        </td>
        </table>        
        </div>
    </center>
    </body>
</html>

3 个答案:

答案 0 :(得分:1)

首先,您不必每行都变换一次。

其次,问题在于:

var kaalk=parseInt(kaal);
var kaalg=parseInt(kaal)%1000;

parseInt使其被截断。

答案 1 :(得分:0)

替换

 parseInt   // This would round the numbers to a integer value

parseFloat // which would give you precision.

另外考虑使用javascript绑定事件而不是内联属性。

<强> Updated Check Fiddle

<强> JS

function arvuta() {
    var kogus1 = document.getElementById("kogus").value;
    if (isNaN(kogus1)) {
        document.getElementById("kaal").value = "...";
        return;
    }
    // Keep it as it is as you seem to be 
    // dividing by 1000 anyways
    var calsg = cals;
    // Get Kilo calories
    var kaal = (calsg * kogus1);
    // Get the kg for calories
    var kaalk = Math.floor(parseInt(kaal, 10) / 1000);
    var kaalg = parseFloat(kaal) % 1000;
    var kaal = kaalk + " kg. " + kaalg + " gr.";

    document.getElementById("kaal").value = kaal;
}

答案 2 :(得分:0)

问题是您使用parseInt,它返回 int eger。

parseFloat可以做到这一点,但不要使用它,因为parseIntparseFloat都很慢,因为它们将字符串解析为数字,而你想将数字转换为字符串!

然后,您应该使用toFixed

myNumber.toFixed(numDecimals)

或者,您也可以使用

myNumber.toPrecision(numDigits)

实施例

(123.456).toFixed(7);     // "123.4560000"  (7 decimals)
(123.456).toPrecision(7); // "123.4560"     (7 digits)
(123.456).toFixed(0);     // "123"          (converted to integer)
(123.456).toPrecision(0); // RangeError: precision 0 out of range