如何在 JQuery 中进行算术运算?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1680032/
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
How to conduct arithmetic operations in JQuery?
提问by Steven
var price = $('#addprice').val();
var pass = $('#pass').val();
var total = $('#totalprice').attr('value')
var left = $('#leftquota').attr('value')
var balance = $('#balance').attr('value')
var tprice = total + price; // total price
var bprice = balance + price; // balance price
var unitprice = bprice / left; // unit price
$('#totalprice').text(tprice);
$('#balance').text(bprice);
$('#unitprice').text(unitprice);
JQuery just treats total, left, balance, tprice, bprice, unitprice,etc. as strings, but actually there are decimals rather than strings. When I apply parseInt() to them, they all become integers, which are not what I want. How to conduct arithmetic operations? The operands are decimals.
JQuery 只处理 total、left、balance、tprice、bprice、unitprice 等。作为字符串,但实际上有小数而不是字符串。当我对它们应用 parseInt() 时,它们都变成了整数,这不是我想要的。如何进行算术运算?操作数是小数。
I use parseFloat(); but it is the same. The operation of var tprice=total+price; just literally conjugates two decimals(strings) together.
我使用 parseFloat(); 但它是一样的。var tprice=total+price 的操作;只是字面上将两个小数(字符串)共轭在一起。
$('#add_price').click(function(){
var price=$('#addprice').val();
var pass=$('#pass').val();
var total=$('#totalprice').attr('value')
var left=$('#leftquota').attr('value')
var balance=$('#balance').attr('value')
total=parseFloat(total);
left=parseFloat(left);
balance=parseFloat(balance);
var tprice=total+price;
var bprice=balance+price;
var unitprice=bprice/left;
$('#totalprice').text(tprice);
$('#balance').text(bprice);
$('#unitprice').text(unitprice);
回答by Anwar Chandra
you can use parseFloatfor this case. it will return float value
在这种情况下,您可以使用parseFloat。它将返回浮点值
example of usage:
用法示例:
var tprice = parseFloat(total) + parseFloat(price);
回答by user187291
Another option is to extend jQuery to be able to read numbers directly from form elements
另一种选择是扩展 jQuery 以能够直接从表单元素读取数字
jQuery.fn.numVal = function() {
return parseFloat(this.val()) || 0;
}
....
var price=$('#addprice').numVal();
var pass=$('#pass').numVal()
回答by Kaze no Koe
回答by Ouahib Abdallah
i was looking for a solution too , and i saw a lot of questions here that doesn't work (even this one) in case someone wondering like me , here is my working solutiuon :
我也在寻找解决方案,我在这里看到很多问题都不起作用(即使是这个问题),以防有人像我一样想知道,这是我的工作解决方案:
$("#apport").keyup(
function(){
var apport = parseFloat($("#apport").val());
var montant = parseFloat($("#montant-financer").val());
var moinmontant = parseFloat(montant) - parseFloat(apport);
$("#montant-financer").val(moinmontant);
}
);
All the id's selector are input
所有id的选择器都是输入
回答by Kamarey
use parseFloat, parseInt, or just place a +before string, if you sure it contains a number:
使用 parseFloat、parseInt,或者在字符串前放置一个+,如果你确定它包含一个数字:
var text = "123.456";
var number = +text; // now 'number' is Number, and not String
Also something tells me that this should work faster than parseInt or parseFloat.
还有一些东西告诉我,这应该比 parseInt 或 parseFloat 工作得更快。
Another way to use Number object:
另一种使用 Number 对象的方法:
var text = "123.456";
var number = Number(text);
But this is the slowest way.
但这是最慢的方式。
More info: http://www.jibbering.com/faq/faq_notes/type_convert.html#tcNumber
更多信息:http: //www.jibbering.com/faq/faq_notes/type_convert.html#tcNumber