0

I have this event that when I input qty and price, the amount will be computed and shown automatically. The value for my Qty is '23635' and my price is '.105'. I am expecting an output of 2481.675 but what I am getting is 2481.6749999999997

Here is my code:

$(".forAmount").blur(function () {
        var Qty = $("#Qty2").val();
        var UnitPrice = $("#UnitPrice2").val();
        var currency = $("#Currency").val();
       
        var total = parseFloat(($("#Qty").val().toString().replace(/,/g, '') * $("#UnitPrice").val().toString().replace(/,/g, '')));
        }
    });

Any idea why I am getting wrong value? Thanks.

Lenard
  • 1

1 Answers1

0

To format a number using fixed-point notation, you can simply use the toFixed method:

$(".forAmount").blur(function () {
        var Qty = $("#Qty2").val();
        var UnitPrice = $("#UnitPrice2").val();
        var currency = $("#Currency").val();
       
        var total = parseFloat(($("#Qty").val().toString().replace(/,/g, '') * $("#UnitPrice").val().toString().replace(/,/g, '')));
        var result  = total.toFixed(3);
        console.log(result);
        }
    });
Aravinth E
  • 449
  • 2
  • 6
  • 29