Change integer value to string or insert mask

Asked

Viewed 31 times

0

I have a function that does a count, only I need to add mask to this value. The final value is 3000 only I need for 3.000;

there is how to change the value of integer to another format and do even so counting within the function itself?

Follows 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>
<div id="numeros">
</div>

1 answer

1


Use the toLocaleString, this way you will work better with formatting numbers or coins.

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>
<div id="numeros">
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.