Change integer value to string or insert mask

3

I have a function that counts, but I need to add a mask to this value. The final value is 3000 only I need for 3,000;

Is there a way to change the integer value to another format and still do the count within the function itself?

Follow function:

function numerosHome(id, inicialValor, valorFinal){
  var inicial = inicialValor;
  var location = document.getElementById(id);
  var contador = setInterval(() => {
      location.innerHTML = inicial;
      inicial++;
      var final = valorFinal +1;
      if(inicial == final){
          clearInterval(contador);
      }
  },0.5);
}
numerosHome('numeros', 2800, 3000);
#numeros{
font-size: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="numeros">
</div>
    
asked by anonymous 22.12.2017 / 17:01

1 answer

1

Use the toLocaleString , that way you will work better with the formatting of numbers or currencies.

function numerosHome(id, inicialValor, valorFinal){
  var inicial = inicialValor;
  var location = document.getElementById(id);
  var contador = setInterval(() => {
      location.innerHTML = inicial.toLocaleString("pt-br");
      inicial++;
      var final = valorFinal +1;
      if(inicial == final){
          clearInterval(contador);
      }
  },0.5);
}
numerosHome('numeros', 2800, 3000);
#numeros{
font-size: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="numeros">
</div>
    
22.12.2017 / 17:12