问题描述
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只处理总计,剩余,余额,tprice,bprice,单价等.作为字符串,但实际上是小数而不是字符串.当我将parseInt()应用于它们时,它们都变成整数,这不是我想要的.如何进行算术运算?操作数是小数.
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.
我使用parseFloat();但是是一样的var tprice = total + price的运算;只是在字面上将两个小数(字符串)共轭在一起.
I use parseFloat(); but it is the same. The operation of var tprice=total+price; just literally conjugates two decimals(strings) together.
$('#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);
推荐答案
,您可以使用 parseFloat 在这种情况下.它将返回浮点值
you can use parseFloat for this case. it will return float value
用法示例:
var tprice = parseFloat(total) + parseFloat(price);
这篇关于如何在JQuery中进行算术运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!