parseFloat sum issue?

Posted on 2012-08-16
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
    LVL 50

    Accepted Solution

    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

    I see now.  Thank you!
    LVL 50

    Expert Comment

    by:Julian Hansen
    You are welcome

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
    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…

    746 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