I am trying to make a function that applies a certain numeric format to an input. Through an onblur a function that applies to an amount that can be written in the following way "145151,65" can be formatted as follows "145.151,65" Currently on the side of the js I have this:
function formatNumber(num) {
if (num == 'Infinity') return '∞';
console.log(num);
num = num.toString().replace(/\$|\,/g, '');
console.log(num);
num = num.toString().replace('.', '');
console.log(num);
num = num.toString().replace(',', '.');
console.log(num);
//if (isNaN(num)) num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num * 100 + 0.50000000001);
cents = num % 100;
if (cents < 10)
cents = "0" + cents;
num = Math.floor(num / 100).toString();
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3) ; i++)
num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
return (((sign) ? '' : '-') + num );
}
And on the html side this
<div class="col-md-4 form-group" style="margin-left:-30px">
<input id="input-sm" type="text" class="form-control col-sm-offset-1 numeros" ng-model="limiteSuperiorAct" id="limiteSuperiorActJD" placeholder="Limite Superior" onblur="this.value = formatNumber(this.value);">
And the result that I get when writing the amount when the onblur is activated is this: "14.515.165". That is to say that I take the decimal part with if it were a whole number and later I format it. If you could help me, thank you.