Javascipt live calculation is not working properly

I want something like when user enter 1 then it should give 1 * 1.2 – 1 = 0.20
where 1.2 is constent but it’s giving 1 * 1.2 – 1 = 0.19999999999999996

I have also tried some other function like tofixed and math.round

<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js”></script>
<script type=”text/javascript”>
jQuery(document).ready(function( $ ){
$(‘input’).keyup(function(){ // run anytime the value changes
var originalPurchasePrice = Number($(‘#originalPurchasePrice’).val()); // get value of field
var vatOwningOnPurchase = Number($(‘#vatOwningOnPurchase’).val()); // convert it to a float
var totalCostPrice = Number($(‘#totalCostPrice’).val());
var fourthValue = Number($(‘#fourth’).val());

//$(‘#total_expenses1’).html(firstValue + secondValue + thirdValue + fourthValue); // add them and output it
//document.getElementById(‘total_expenses2’).value = firstValue + secondValue;
document.getElementById(‘vatOwningOnPurchase’).value = originalPurchasePrice * 1.2 – originalPurchasePrice;
$(‘#totalCostPrice’).html(originalPurchasePrice * 1.2 – originalPurchasePrice);
// add them and output it
});
});
</script>
</head>
<body>
<form>
<p>Original Purchase Price of Cask (*)</p>
<input id=’originalPurchasePrice’ type=”text” class=”form-control formBlock” name=”bus_ticket” placeholder=”Bus Ticket…” required/><br />
<p>VAT owing on purchase price</p>
<input id=’vatOwningOnPurchase’ type=”text” class=”form-control formBlock” name=”plane_ticket” placeholder=”Plane Ticket…” required/><br />
Total : <span id=”totalCostPrice”></span>
</form>
</body>
</html>

Read more here:: Javascipt live calculation is not working properly

Leave a Reply

Your email address will not be published. Required fields are marked *