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

x
?
Solved

parseFloat sum issue?

Posted on 2012-08-16
3
Medium Priority
?
549 Views
Last Modified: 2012-08-16
Can someone please help me understand why this script returns 1037.56137.56 when I am trying to have it return 2075.12.  I thought parseFloat converts strings to decimal numbers?  I am missing something here...
function doSplitCalc()
{
    var intrs2Count = <%=intrs2Count %>
    //alert(intrs2Count);
     
    var arrSplits = new Array();

    for (i = 0; i < intrs2Count; i++)
    {
        
        arrSplits[i]  = parseFloat(document.getElementsByName('txtTranAmt')[i].value.replace(',','')).toFixed(2)  // first element in DOM (index 0) with id="txtTranAmt"
        //alert(arrSplits[i]);
    }
    
    var SplitTotal = parseFloat(arrSplits[0]).toFixed(2) + parseFloat(arrSplits[1]).toFixed(2)
    alert(SplitTotal);
}

Open in new window

TIA,
Andrew
0
Comment
Question by:Ahelbling
  • 2
3 Comments
 
LVL 61

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 38302397
It is concatenating the strings but confused

 1037.56137.56

I assume should have been
 1037.56 + 137.56 = 1175.12 not 2075.12 (900 out)

But your problem is here
var  SplitTotal = parseFloat(arrSplits[0]).toFixed(2) + parseFloat(arrSplits[1]).toFixed(2);

should be
var  SplitTotal = parseFloat(arrSplits[0]) + parseFloat(arrSplits[1])

toFixed converts to a string so the + is interpreted as concatenate not add.
0
 

Author Comment

by:Ahelbling
ID: 38302417
I see now.  Thank you!
0
 
LVL 61

Expert Comment

by:Julian Hansen
ID: 38302474
You are welcome
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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

575 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