Home > front end >  Not showing result with decimal discount value
Not showing result with decimal discount value

Time:07-23

I have a discount jquery calculator plugin.It is working. But percentage is not working with decimal values like Eg: 5.45%, 10.50% , 15.70%, 20.75% etc.. I created a Codepen project for it. Please check this Codepen link https://codepen.io/coderco/pen/gOeReMQ . Here is my codes. Please help me..

HTML

<input type="text" id="Amount" value="">
<input type="text" value="10.45" id="Discount">
<input type="text" id="Result">

Script

$(document).on("change keyup blur", "#Discount", function() {
    var main = $('#Amount').val();
    var disc = $('#Discount').val();
    var dec = (disc / 100).toFixed(2); //its convert 10 into 0.10
    var mult = main * dec; // gives the value for subtract from main value
    var discont = main - mult;
    $('#Result').val(discont);
});

CodePudding user response:

It's because of toFixed(2). When you divide the value by 100 then there are 4 decimal places.

For example:
Amount = 100, Discount = 10.45
dec = (10.45/100) = 0.1045 (Calling toFixed(2) changes it to 0.10 that means 10 instead of 10.45).

In my suggestion add toFixed(2) to the final answer (discount), Instead of while calculating.

  • Related