最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to add integer and float using javascript? - Stack Overflow

programmeradmin6浏览0评论

I am using the below code to add integer and float value using JavaScript. But I can't do this, it returns NaN. I am new to development. Please help me to solve this.

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);

I am using the below code to add integer and float value using JavaScript. But I can't do this, it returns NaN. I am new to development. Please help me to solve this.

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);
Share Improve this question edited Feb 28, 2013 at 5:16 mrk 5,1273 gold badges28 silver badges42 bronze badges asked Feb 28, 2013 at 5:06 user2110618user2110618 1193 gold badges4 silver badges9 bronze badges 2
  • Is this your actual code? What is Numbers? – Blender Commented Feb 28, 2013 at 5:08
  • 1 Also the Number() function will return NAN if the arguments value cannot be converted to a number. Are you sure lbl_bal_total.innerHTML is always numerical characters. developer.mozilla/en-US/docs/JavaScript/Reference/… – Jesse Lee Commented Feb 28, 2013 at 5:24
Add a ment  | 

4 Answers 4

Reset to default 2

Try this:

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

Try this. Data type is Number not 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);

if you are not sure about the values of lbl_bal_total.innerHTML and lbl_bal_others.value then you can check it using isNaN

var num = parseFloat(lbl_bal_total.innerHTML) + parseFloat(lbl_bal_others.value);
lbl_bal_total.innerHTML = Math.round(num);
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);
发布评论

评论列表(0)

  1. 暂无评论