I have two numerical input fields where each value is used in a calculation and displayed in an output.The user will input their own numbers, but each input has a starting value on load. Because the value for input field 1 is large, I wanted commas. I have the script for adding commas and script for the calculation. The problem is I want the large value field’s number on load to have a comma. It wouldn’t on load so I added a comma to the input value=“200,000” The comma is there on load, but then the calculation wont work. If I take out the comma from the value=“200000” the calculation works, but the on load 200000 doesn’t have a comma. Any help to have a comma on the load value and have the calculation still work would be appreciated. Thank you.

My else statement isn’t working, it breaks the commas. Also, if the user puts the curser behind the “2” in 200,000 and hits backspace to delete because lets say they want to make the “2” a “4”, the cursor goes to the end of the number; i.e 2|00,000> backspace > 00,000|

$("input[data-type='number']").keyup(function(event){
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
else if(isNaN(units)){ return "ERR";}
else{return false;
}
var $this = $(this);
var num = $this.val().replace(/,/gi, "");
var num2 = num.split(/(?=(?:\d{3})+$)/).join(",");
console.log(num2);
$this.val(num2);
});
Also, once the number of input digits gets to 8, the calculation is off:
$('#quantity,#apply_time').keyup(function() {
var units = $("#quantity").val();
var seconds = $("#apply_time").val();
var convert = 60;
var total = Math.round (units * seconds / convert / convert);
$("#hours").val(total); // sets the total hours input to the quantity * seconds
});

I have not come across a programming language that will perform arithmetic on a number with commas in it. You need one variable without the commas to do arithmetic on and another with commas in to output.

I have always stored numbers “clean” (eg. integers or floats) so that it is easier to do math with them, and then added commas, dollar signs etc. to the displayed values

That’s part of the reason I’m having some trouble conceptualizing this problem. It’s the reverse of what I have always done.

But if I take out the add commas JS and have my starting value=“200,000”, the calculation still works. It works onload and if let’s say the user replaces the 2 with a 4 to make 400,000.