Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JavaScript: parseFloat

Posted on 2006-04-27
5
Medium Priority
?
1,835 Views
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;
    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.
0
Comment
Question by:lnum
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
dbritt earned 200 total points
ID: 16559159
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
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16564543
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
 
LVL 15

Expert Comment

by:dbritt
ID: 16564711
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
Suggested Courses

580 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