jQuery 在 JavaScript 中不使用 parseFloat 无法正确添加两个数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16117985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot add two numbers correctly without using parseFloat in JavaScript
提问by Lakshmana Kumar
I came across this problem. It adding numbers using parseFloat or parseInt. IF textbox1 value is 4 and textbox2 value is 2 then i got output as (see script)
我遇到了这个问题。它使用 parseFloat 或 parseInt 添加数字。如果 textbox1 值为 4 而 textbox2 值为 2 然后我得到输出(见脚本)
My doubt is why in additionalone
我的疑问是为什么除了单独
parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())
parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())
gives correct value but
给出正确的值,但
parseFloat($('#txt1').val() + $('#txt2').val())
parseFloat($('#txt1').val() + $('#txt2').val())
is not giving correct value whereas
没有给出正确的价值,而
parseFloat($('#txt1').val() - $('#txt2').val())
,parseFloat($('#txt1').val() / $('#txt2').val())
,parseFloat($('#txt1').val() * $('#txt2').val())
parseFloat($('#txt1').val() - $('#txt2').val())
,parseFloat($('#txt1').val() / $('#txt2').val())
,parseFloat($('#txt1').val() * $('#txt2').val())
are giving correct value. Its simple but i couldn't find solution.
正在给出正确的值。它很简单,但我找不到解决方案。
=====jQuery
======jQuery
function Calculate() { //--> Output
$('#lbl1').html(parseFloat($('#txt1').val() + $('#txt2').val())); //--> 42
$('#lbl2').html(parseFloat($('#txt1').val()) + parseFloat($('#txt2').val())); //--> 6
$('#lbl3').html(parseFloat(4 + 2)); //--> 6
$('#lbl4').html(parseFloat($('#txt1').val() - $('#txt2').val())); //--> 2
$('#lbl5').html(parseFloat($('#txt1').val() * $('#txt2').val())); //--> 8
$('#lbl6').html(parseFloat($('#txt1').val() / $('#txt2').val())); //--> 2
}
=====HTML
======HTML
<table>
<tr>
<td>
<input type="text" id="txt1" />
</td>
<td>
<input type="text" id="txt2" />
</td>
</tr>
<tr>
<td>
<input type="button" value="Calculate" onclick="Calculate()" />
</td>
<td>
<label id="lbl1">
</label>
|
<label id="lbl2">
</label>
|
<label id="lbl3">
</label>
|
<label id="lbl4">
</label>
|
<label id="lbl5">
</label>
|
<label id="lbl6">
</label>
</td>
</tr>
</table>
回答by Sirko
$.val()
returns a string value.
$.val()
返回一个字符串值。
So in your first example you convert both returned strings to numbers and the calculation is fine.
因此,在您的第一个示例中,您将两个返回的字符串都转换为数字,并且计算很好。
If you use parseFloat($('#txt1').val() + $('#txt2').val())
the +
does not work as the arithmetic operator, but as a string concatenation. So you concatenate both strings and convert them afterwards, which gives a wrong result.
如果你使用parseFloat($('#txt1').val() + $('#txt2').val())
了+
不工作的算术运算符,但作为一个字符串连接。所以你连接两个字符串并在之后转换它们,这给出了错误的结果。
The examples using -
will work, as there is no string operation using -
and by thus alls values get implicitly converted to a number beforethe operation is applied.
使用的示例-
将起作用,因为没有使用字符串操作-
,因此所有值在应用操作之前都会隐式转换为数字。
回答by PSR
$('#txt1').val() + $('#txt2').val()
it gives String value
$('#txt1').val() + $('#txt2').val()
它给出字符串值
you can not use - , * , /operator on strings
你不能在字符串上使用 - , * , /operator
parseFloat($('#txt1').val()), parseFloat($('#txt2').val())
returns numbers not strings
parseFloat($('#txt1').val()), parseFloat($('#txt2').val())
返回数字而不是字符串