javascript – How to change the numeric format to an input

I'm 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

And the result that gives me 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.