How to apply a numeric quantity format to an input

0

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.

    
asked by Alejandro Pereira 27.11.2018 в 16:39
source

2 answers

1

I have thought of the following solution, since the "whole" part is already controlled, and the decimal part does not have to be modified, you can cut the string in two with the following code:

  var comma = num.split(',')[1];
  num = num.split(',')[0];

In this way, your code would only affect the entire part. Finally, I put the following check at the end, to verify that the number actually has a decimal part:

var result = '';
  if (comma){
    result = ((sign) ? '' : '-') + num +','+comma;
  } else {
    result = ((sign) ? '' : '-') + num;
  }
return result;

If you do not have it, your code would only affect the whole part.

I'll give you the proof that I used to prove it:

function formatNumber(num) {
  var comma = num.split(',')[1];
  num = num.split(',')[0];
  console.log("Coma:"+comma);
if (num == 'Infinity') return '&#x221e;';
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));
  var result = '';
  if (comma){
    result = ((sign) ? '' : '-') + num +','+comma;
  } else {
    result = ((sign) ? '' : '-') + num;
  }
return result;
}
<input id="input-sm" type="text" id="limiteSuperiorActJD" placeholder="Limite Superior" onblur="this.value = formatNumber(this.value);">

I hope it helps you. Greetings.

    
answered by 27.11.2018 / 17:14
source
-1

Here is an example of how you could do it. It is not the great code, it is commented so that it is understood. Greetings!

function toCurrency(string){
        return string.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    }
    
 function check(obj)
 {
    //Reemplaxamos la coma por un punto y le asignamos presicion de 2 decimales.
     let val = parseFloat(obj.value.replace(",",".")).toFixed(2);
     
     //Aplicamos el formato deseado
     val = toCurrency(val);
     //Actualizamos el valor
     obj.value= val;

 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" value="" onblur="check(this)" placeholder="Ingrese un numero">
    
answered by 27.11.2018 в 17:45