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
4 Answers
Reset to default 2Try 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);