I have a calculation where I need to multiply the values of two different inputs, add the value of a third, and put that value into the other input. The first input will sometimes have a decimal point number (EX: 3.2) while the second number has commas for thousands, millions, etc... I then need to properly multiply those two numbers into each other, add the third input value, and put the result into the fourth input correctly.
Right now the behavior is a bit strange, sometimes the decimal number 3.2, for example, will be used as 32. I have tried a few different solutions to similar problems, but they did not produce the results I need or also had odd behavior. The toFixed() method sometimes creates numbers that are far too large. It may turn millions into quadrillions, for example.
EDIT: Sorry, I forgot to mention I have a third input value that needs to be added to the other two after multiplication. THIS is where the issue appears.
$(document).on("input", "#input-1, #input-2, #input-3", function() {
var val1 = $("#input-1").val();
var val2 = parseFloat($("#input-2").val().replace(/,/g, ''));
var val3 = $("#input-3").val();
var total = (val1 * val2) + val3;
total = total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
$("#input-4").val(total).trigger("input");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<input id="input-1" type="text" value="3.2" />
<input id="input-2" type="text" value="1,000,000" />
<input id="input-3" type="text" value="3" />
<input id="input-4" type="text" value="0" />
</div>