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

vktxenjb  于 2023-10-17  发布在  jQuery
关注(0)|答案(4)|浏览(97)

我使用下面的代码添加整数和浮点值使用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);
iibxawm4

iibxawm41#

试试这个:

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

jchrr9hc2#

试试这个。数据类型是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来检查它。

68de4m5k

68de4m5k3#

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

hi3rlvi24#

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

相关问题