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).value;
        if(tmp==""){ tmp = 0; }
        sum = sum + parseFloat(tmp);
    }
    eval(document.getElementById("txtSum"+col)).innerHTML = 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,000". 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.
lnumAsked:
Who is Participating?
 
dbrittCommented:
Your number is too big for JavaScript to handle as a number, check Number.MAX_VALUE

The usual way to handle this is to do some algorithm to use strings. Here's one site I found with an example of adding large numbers as strings:

http://www.trans4mind.com/personal_development/JavaScript/longnumAdd.htm


Hope this helps.
0
 
Pravin AsarPrincipal Systems EngineerCommented:
Are you familar with Number object in Javascript ?


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

0
 
dbrittCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.