Search code examples
javascriptasp.net

while using parseFloat , not able to calculate the amount more than 1 million


While using check box section in asp.net ,grid view,with the following code. not able to calculate the sum of the amount , eg,

  • if the grid row having the value with 1,001,870.00 then,getting the result as 1

  • if the grid row having the value with 976,000 then, getting the result as 976,000

am using the following script function,

function GetSelected() {
    debugger;
    
    var grid = document.getElementById("<%=grvGroup.ClientID%>");

    var checkBoxes = grid.getElementsByTagName("INPUT");
    var amountFC = 0;
    var amountVAT = 0;
    var amountSUMTOT = 0;
    var countSelect = 0;
    var totalValAmount = 0;
    
    if (grid.rows.length > 0) {

        for (var i = 1; i < grid.rows.length; i++) {
            var checkBoxes = grid.rows[i].getElementsByTagName('input');
            var checkedid = checkBoxes[0].getAttribute('id');
            var checkbox = document.getElementById(checkedid);

            if (checkbox.checked) {
                Amt = grid.rows[i].cells[5].innerText.replace(",", "");
                amountFC += parseFloat(Amt);
                Amt = grid.rows[i].cells[6].innerText.replace(",", "");
                amountVAT += parseFloat(Amt);
                
                countSelect++;
            }
        }
        jQuery('#<%=txtTotalAmount.ClientID %>').val(amountFC);
        jQuery('#<%=txtTotalVat.ClientID %>').val(amountVAT);
        var totalAmount = parseFloat(jQuery('#<%=txtTotalAmount.ClientID %>').val());
        var totalVat = parseFloat(jQuery('#<%=txtTotalVat.ClientID %>').val());
        var totalSum = totalAmount + totalVat;


        jQuery('#<%=txtPayableAmount.ClientID %>').val(totalSum);

    }
    if (grid.rows.length - 1 == countSelect) {
        document.getElementById("ContentPlaceHolder1_checkallselect").checked = true;
    }
    else {
        document.getElementById("ContentPlaceHolder1_checkallselect").checked = false;
    }
}

Solution

  • In the documentation of JS function replace() we can read the following:

    A string pattern will only be replaced once. To perform a global search and replace, use a regular expression with the g flag, or use replaceAll() instead.

    These are the two options you have to remove all , from your number.

    ReplaceAll()

    grid.rows[i].cells[5].innerText.replaceAll(",", "");
    

    Using REGEX

    grid.rows[i].cells[5].innerText.replace(/,/g, "");
    

    Docs

    Replace

    Replace all