如何使用javascript添加整数和浮点数?

时间:2013-02-28 05:06:36

标签: javascript jquery floating-point integer

我使用下面的代码使用JavaScript添加整数和浮点值。但我不能这样做,它会返回NaN。我是发展新手。请帮我解决这个问题。

lbl_bal_total.value = Numbers(lbl_bal_total.innerHTML) + Numbers(lbl_bal_others.value);
//lbl_bal_total.value = 1568 + .25; // Error lbl_bal_total value is NaN
lbl_bal_total.innerHTML = Math.round(lbl_bal_total.value);

3 个答案:

答案 0 :(得分:1)

试试这个:

lbl_bal_total.innerHTML = Math.round(parseFloat(lbl_bal_total.innerHTML) + parseFloat(lbl_bal_others.value));

答案 1 :(得分:0)

var num = parseFloat(lbl_bal_total.innerHTML) + parseFloat(lbl_bal_others.value);
lbl_bal_total.innerHTML = Math.round(num);

答案 2 :(得分:0)

试试这个。数据类型为Number而非Numbers

 lbl_bal_total.value = Number(lbl_bal_total.innerHTML) + Number(lbl_bal_others.value);
    //lbl_bal_total.value = 1568 + .25; // Error lbl_bal_total value is NaN
    lbl_bal_total.innerHTML = Math.round(lbl_bal_total.value);

如果您不确定lbl_bal_total.innerHTMLlbl_bal_others.value的值,则可以使用isNaN

进行检查