JavaScript计算中的结果不正确 [英] Incorrect result in JavaScript calculation

查看:123
本文介绍了JavaScript计算中的结果不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript很陌生,想把我的一个计算电子表格转换成我可以在网页上使用的东西。但是,计算结果不正确。

I'm fairly new to JavaScript and wanted to convert one of my calculation spreadsheets to something I can use on a web page. However, the calculation gives me an incorrect result.

这是我的JavaScript:

Here's my JavaScript:

<script type="text/javascript" language="JavaScript">
        function calc() {
            var onerepmax = document.wodCalculate.oneRepMax.value;
            var percent = document.wodCalculate.percentOfOneRepMax.value / 100;
            var addkg = document.wodCalculate.plusKg.value;
            var zwischenschritt = onerepmax * percent;
            var gewicht = zwischenschritt + addkg;
            document.getElementById("weight").innerHTML = gewicht;
        };
</script>

这里是HTML:

<form action="" id="wodCalculate" name="wodCalculate">
        <table cellspacing="0" cellpadding="10" border="0">
            <tr><td>1 Rep Max</td><td><input type=text name="oneRepMax" value="">&nbsp;kg<br></td></tr>
            <tr><td>% vom 1RM</td><td><input type=text name="percentOfOneRepMax" value="">&nbsp;%<br></td></tr>
            <tr><td>Plus x kg</td><td><input type=text name="plusKg" value="">&nbsp;kg<br></td></tr>
            <tr><td><input type="button" value="Calculate" onClick="calc()"></td></tr>
        </table>
</form>
<div id="weight">Weight</div>

它可以正常工作,直到onerepmax乘以百分比。但是,一旦它到达乘法结果中添加addkg(即给我gewicht),结果就会变得不正确。

It works fine up to the point of multiplying the "onerepmax" with the "percent". However, once it gets to the point of adding the "addkg" to the result of the multiplication (i.e. giving me the "gewicht"), the result becomes incorrect.

例如,我想获得100公斤的10%并增加10公斤。而不是20公斤,计算结果是1010。

For example, I want to get 10% of 100kg and add 10kg. Instead of 20kg, the calculation result is 1010.

感谢您的帮助!

推荐答案

输入始终是一个字符串,所以 + 最终成为字符串连接(10+101010,而 10 + 10 20 )。

The value of an input is always a string, so + ends up being string concatenation ("10" + "10" is "1010", as opposed to 10 + 10 which is 20).

如果你正在使用输入类型=数字(OP不是,但其他人找到了这个答案可能)和浏览器支持它们,您可以使用 valueAsNumber 而不是:

If you're using an input type="number" (the OP isn't, but others finding this answer may) and the browser supports them, you can use valueAsNumber instead:

var onerepmax = document.wodCalculate.oneRepMax.valueAsNumber;

如果您使用的是 type =text或浏览器不支持 valueAsNumber

If you're using type="text" or the browser doesn't support valueAsNumber:

您可以使用<$ c转换用户输入值$ c> parseInt(value,10)(10 =十进制,基数为10)如果它们是整数,例如:

You can convert user-input values using parseInt(value, 10) (the 10 = decimal, base 10) if they're meant to be whole numbers, e.g.:

var onerepmax = parseInt(document.wodCalculate.oneRepMax.value, 10);

这只是一个选项,但是你有几个:

That's just one option, though, you have several:


  • 一元 + 运算符: value = + value 将使用JavaScript引擎的标准规则将字符串强制转换为数字。该数字可以具有小数部分(例如, +1.50 1.5 )。字符串中的任何非数字(科学计数法的 e 除外)都会产生结果 NaN 。此外, + 0 ,这可能不直观。

  • The unary + operator: value = +value will coerce the string to a number using the JavaScript engine's standard rules for that. The number can have a fractional portion (e.g., +"1.50" is 1.5). Any non-digits in the string (other than the e for scientific notation) make the result NaN. Also, +"" is 0, which may not be intuitive.

var onerepmax = +document.wodCalculate.oneRepMax.value;


  • Number 函数: value =数字(值)。与 + 相同。

    var onerepmax = Number(document.wodCalculate.oneRepMax.value);
    


  • parseInt 函数,通常使用基数(数字基数): value = parseInt(value,10)。这里的缺点是 parseInt 转换它在字符串开头找到的任何数字,但忽略字符串后面的非数字,所以 parseInt(100asdf,10) 100 ,而不是 NaN 。顾名思义, parseInt 只解析整数。

  • The parseInt function, usually with a radix (number base): value = parseInt(value, 10). The downside here is that parseInt converts any number it finds at the beginning of the string but ignores non-digits later in the string, so parseInt("100asdf", 10) is 100, not NaN. As the name implies, parseInt parses only a whole number.

    // (Same as the first one above)
    var onerepmax = parseInt(document.wodCalculate.oneRepMax.value, 10);
    


  • parseFloat 函数: value = parseFloat(value)。允许小数值,并始终以十进制(从不八进制或十六进制)工作。同样的事情 parseInt 是否在字符串末尾有垃圾, parseFloat(123.34alksdjf) 123.34

  • The parseFloat function: value = parseFloat(value). Allows fractional values, and always works in decimal (never octal or hex). Does the same thing parseInt does with garbage at the end of the string, parseFloat("123.34alksdjf") is 123.34.

    var onerepmax = parseFloat(document.wodCalculate.oneRepMax.value);
    


  • 因此,选择适合您使用的工具案件。 : - )

    So, pick your tool to suit your use case. :-)

    这篇关于JavaScript计算中的结果不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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