lnum
asked on
JavaScript: parseFloat
Hi all,
I have input textbox which keep number(22,0) in oracle database so I set maxlength of this textbox to 22. When user input and goto next textbox I will sum all of textbox in the last row of each column using javascript function.
My Function code is as follow:
-------------------------- ---------- ---------- ---------- ----------
function sumCol(col) {
var sum,tmp;
sum=0;
for (r=1;r<=12;r++) {
tmp = eval("document.form2.t"+r+ "_"+col).v alue;
if(tmp==""){ tmp = 0; }
sum = sum + parseFloat(tmp);
}
eval(document.getElementBy Id("txtSum "+col)).in nerHTML = formatNumber(sum,",");
}
-------------------------- ---------- ---------- ---------- ----------
The problem is : I try to test by input the maximum value to 22 digits. I input "1234567890123456789012" and function calculate just "1" in the last row. I try with "123456789012345678901", the result is "123,456,789,012,345,680,0 00". The result will correct if I input just 16 digits like "1234567890123456" [then the result is 1,234,567,890,123,456].
Why it happened like this and how should I do. Please help me.
Thanks a lot.
I have input textbox which keep number(22,0) in oracle database so I set maxlength of this textbox to 22. When user input and goto next textbox I will sum all of textbox in the last row of each column using javascript function.
My Function code is as follow:
--------------------------
function sumCol(col) {
var sum,tmp;
sum=0;
for (r=1;r<=12;r++) {
tmp = eval("document.form2.t"+r+
if(tmp==""){ tmp = 0; }
sum = sum + parseFloat(tmp);
}
eval(document.getElementBy
}
--------------------------
The problem is : I try to test by input the maximum value to 22 digits. I input "1234567890123456789012" and function calculate just "1" in the last row. I try with "123456789012345678901", the result is "123,456,789,012,345,680,0
Why it happened like this and how should I do. Please help me.
Thanks a lot.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
prav, when I tried doing num.valueOf() on the number that was too large, it still displayed the wrong value, that's why I didn't suggest it.
e.g.
var num1 = new Number ('1234567890123456789012')
var num2 = new Number ('9999999999999999999999')
var num3 = new Number(num1.valueOf()+ num2.valueOf());
alert ('Value of num3 ' + num3.toString());