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


parseFloat sum issue?

Posted on 2012-08-16
Medium Priority
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 %>
    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"
    var SplitTotal = parseFloat(arrSplits[0]).toFixed(2) + parseFloat(arrSplits[1]).toFixed(2)

Open in new window

Question by:Ahelbling
  • 2
LVL 61

Accepted Solution

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


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.

Author Comment

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

Expert Comment

by:Julian Hansen
ID: 38302474
You are welcome

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