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
Medium Priority
549 Views
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)
}
``````
TIA,
Andrew
0
Question by:Ahelbling
• 2

LVL 61

Accepted Solution

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)

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

ID: 38302417
I see now.  Thank you!
0

LVL 61

Expert Comment

ID: 38302474
You are welcome
0

## Featured Post

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
Course of the Month15 days, 7 hours left to enroll