Ayman
Ayman

Reputation: 51

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>

Upvotes: 0

Views: 50

Answers (2)

jwatts1980
jwatts1980

Reputation: 7356

Try changing:

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

to

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

Upvotes: 1

pranavjindal999
pranavjindal999

Reputation: 3047

string concatenation happening with + operator.

parseInt or parseFloat before adding

Upvotes: 1

Related Questions