JavaScript: parseFloat

Posted on 2006-04-27
Last Modified: 2008-02-07
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;
    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.
Question by:lnum
    LVL 15

    Accepted Solution

    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:

    Hope this helps.
    LVL 28

    Expert Comment

    by:Pravin Asar
    Are you familar with Number object in Javascript ?


    var num1 = new Number ('1234567890123456789012');
    var num2 = new Number ('9999999999999999999999');
    var num3 = new Number(num1.valueOf()+ num2.valueOf());

    alert ('Value of num3  ' + num3.toString());

    LVL 15

    Expert Comment

    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.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now