Incorrect result in JavaScript calculations

the below java-script code supposed to get order amount and total amount of order after adding the delivery charge to the order amount.

Example

Order Amount = 25
Delivery charge = 5
Total Amount  = 30

Delivery charge comes from label control in asp.

But when i applied the blow code i got these results.

Order Amount = 25
Delivery charge = 5
Total Amount  = 255

JavaScript

<script>
        $(document).ready(function () {
            function multInputs() {
                var $mult = 0;
                var $multGrand = 0;
                var $multCharge = 0;
                $("tr.txtMult").each(function () {
                    var $UnitPrice = $('.UnitPrice', this).val();
                    var $Quantity = $('.Quantity', this).val();
                    var $total = (($UnitPrice) * ($Quantity));

                    $mult += $total;
                    $multCharge = $mult + $('#<%= lbldcharge.ClientID %>').text();

                });

                $("tr.txtMult").each(function () {
                    var $UnitPrice = $('.UnitPrice', this).val();
                    var $Quantity = $('.Quantity', this).val();
                    var $total = (($UnitPrice) * ($Quantity));

                    $('.multTotal', this).text(parseFloat($total).toFixed(2));

                });
                $(".lblGrandAmount").text(parseFloat($mult).toFixed(2));
                $(".lblChrageAmount").text(parseFloat($multCharge).toFixed(2));

            }

            $(".txtMult input").on('keyup mouseup', multInputs);
            function multInputs() {
                var $mult = 0;
                var $multGrand = 0;
                var $multCharge = 0;
                $("tr.txtMult").each(function () {
                    var $UnitPrice = $('.UnitPrice', this).val();
                    var $Quantity = $('.Quantity', this).val();
                    var $total = (($UnitPrice) * ($Quantity));

                    $mult += $total;
                    $multCharge = $mult + $('#<%= lbldcharge.ClientID %>').text();


                });

                $("tr.txtMult").each(function () {
                    var $UnitPrice = $('.UnitPrice', this).val();
                    var $Quantity = $('.Quantity', this).val();
                    var $total = (($UnitPrice) * ($Quantity));


                    $('.multTotal', this).text(parseFloat($total).toFixed(2));

                });
                $(".lblGrandAmount").text(parseFloat($mult).toFixed(2));

                $(".lblChrageAmount").text(parseFloat($multCharge).toFixed(2));
            }
        });

    </script>

Label

<asp:Label ID="lbldcharge" runat="server" Text="5"></asp:Label>

Answers 1

  • Try changing:

    $multCharge = $mult + $('#<%= lbldcharge.ClientID %>').text()
    

    to

    $multCharge = $mult + parseFloat($('#<%= lbldcharge.ClientID %>').text())
    

Related Articles