im new to JS/jQuery & im stuck with this for days, no solution worked this far..
Code for debugging here: http://jsfiddle.net/qgw4ckmz/1
I already trying most of javascript number formatting, but none of them work in my case, What im tryng to achieve is thousand separator output, ex: 1000000
become 1,000,000
, 1000
become 1,000
etc.
it worked fine if the number is 100000 or below, but after 1000000,
it shows error on browser console:
The specified value "1,000,000" cannot be parsed, or is out of range.
i tried this but doesnt work (only able to parse up to 100.000 ):
<script type="text/javascript">
$(document).ready(function(){
var totalsum = 0;
$(".barangx").each(function(){
var inputval = $(this).val();
if($.isNumeric(inputval)){
totalsum +=parseFloat(inputval);
}
});
$('.totalx').val(totalsum.toLocaleString('id-ID'));
});
i also tried this code whis is mentioned here but the result are the same (only able to parse up to 100.000 ): http://www.mredkj.com/javascript/numberFormat.html
<script type="text/javascript">
$(document).ready(function(){
function addCommas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
var totalsum = 0;
$(".barangx").each(function(){
var inputval = $(this).val();
if($.isNumeric(inputval)){
totalsum +=parseFloat(inputval);
}
});
totalsum = addCommas(totalsum);
$('.totalx').val(totalsum);
});
</script>
COMPLETE CODE
<!DOCTYPE html>
<html lang="en">
<body>
<label>Val 1</label>
<div class="ml-auto"><input type="number" value="10000" class="input barangx" readonly step="0.01" id="id_form-0-total"></div>
<label>Val 2 - Change this fieild to 1000000</label>
<div><input type="number" value="10000" class="input barangx" step="0.01"></div>
<label>Val 1 + Val 2=</label>
<input type="number" name="total" step="0.01" class="input totalx" readonly required id="id_total">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".barangx").keyup(function(){
var totalsum = 0;
$(".barangx").each(function(){
var inputval = $(this).val();
inputval = inputval.replace(/,/g, "");
if($.isNumeric(inputval)){
totalsum +=parseFloat(inputval);
}
});
totalsum = totalsum.toLocaleString('id-ID');
$('.totalx').val(totalsum);
});
});
</script>
</body>
</html>