Format string or float for currency

1

I've been researching this but I have not found a solution. I have for example a float: 2087500 , I want to leave it as follows $ 20,875.00

I found several methods of passing float to coin but everyone has a period (.) before the last two digits in order to work, so 20875.00 . I tried using replace (/ ([0-9] {2}) $ / g, ". $ 1") to put this point but it is only added in String. And as I said, the functions I found work only with float.

Could anyone help me with a functional solution please.

    
asked by anonymous 26.06.2017 / 19:22

3 answers

1

It would be best to use Intl.NumberFormat function of object Intl , it makes it easy to format numbers according to the language, eg:

//Instanciando o objeto
var formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  minimumFractionDigits: 2,
});
//pega o valor no campo e transforma em float
var valor = parseFloat(document.getElementById("moeda").value);
/*FORMATA CONFORME CONFIGURAÇÕES DEFINIDAS NO formatter*/
var formatado = formatter.format(valor); 
//Coloca o valor formatado no campo resultado
document.getElementById("resultado").value = formatado;
<input type="text" id="moeda" value="20875.00">
<input type="text" id="resultado" value="">

//Instanciando o objeto
var formatter = new Intl.NumberFormat('pt-BR', {
  style: 'currency',
  currency: 'BRL',
  minimumFractionDigits: 2,
});
//pega o valor no campo e transforma em float
var valor = parseFloat(document.getElementById("moeda").value);
/*FORMATA CONFORME CONFIGURAÇÕES DEFINIDAS NO formatter*/
var formatado = formatter.format(valor); 
//Coloca o valor formatado no campo resultado
document.getElementById("resultado").value = formatado;
<input type="text" id="moeda" value="20875.00">
<input type="text" id="resultado" value="">

NOTE: In order to use this way, both the string and the float you want to format must contain the correct value with decimal places, for example:

  

1 = $ 1.00
  1.0 = $ 1.00
  10 = $ 10.00

    
26.06.2017 / 20:50
2

Functions with and without input.

var mask = {
   money: function() {
      var el = this
      ,exec = function(v) {
         v = v.replace(/\D/g,"");
         v = new String(Number(v));
         var len = v.length;
         if (1== len)
            v = v.replace(/(\d)/,"0,0$1");
         else if (2 == len)
            v = v.replace(/(\d)/,"0,$1");
         else if (len > 2) {
            v = v.replace(/(\d{2})$/,',$1');
            if (len > 5) {
               var x = len - 5
               ,er = new RegExp('(\d{'+x+'})(\d)');
               v = v.replace(er,'$1.$2');
            }
         }
         return v;
      };
      setTimeout(function(){
         el.value = exec(el.value);
      },1);
   }
}

$(function(){
   $('input').bind('keypress',mask.money)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text"  value=""/>

function mascara(v) {
  v = v.replace(/\D/g,"");
  v = new String(Number(v));
  var len = v.length;
  if (1== len)
    v = v.replace(/(\d)/,"0,0$1");
  else if (2 == len)
    v = v.replace(/(\d)/,"0,$1");
  else if (len > 2) {
    v = v.replace(/(\d{2})$/,',$1');
  if (len > 5) {
  var x = len - 5
  ,er = new RegExp('(\d{'+x+'})(\d)');
  v = v.replace(er,'$1.$2');
  }
}
return v;
}
console.log(mascara('2087500'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
26.06.2017 / 19:37
0

Just divide by 100 would not it be enough?

var intl = new Intl.NumberFormat("en-US", { 
  style: "currency", 
  currency: "USD"
});
var valor = 2087500;
var moeda = intl.format((valor / 100));
console.log(valor, moeda);
    
26.06.2017 / 20:44