Complete decimal places ",00" Javascript

Asked

Viewed 4,704 times

3

People I have the following function that I am using to use Coin mask

<input class="valr-parc" type="text" name="valr-parc" />

<script>
    String.prototype.Moeda = function() {
        var v = this;
        v = v.replace(/\D/g,'')
        v = v.replace(/(\d{1})(\d{1,2})$/, "$1,$2")
        v = v.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.')
        v = v.replace(/^(\d)/g,"R$ $1")
        return v;
    }
</script>

<script type="text/javascript">

(function(view) {
    var valr_parc  = document.getElementsByClassName("valr-parc")[0];


    valr_parc.onkeyup =  function(){
        this.value = this.value.Moeda();
    };

})(this);
</script>

Only that I wanted to implement in this function . Currency as follows: When I enter only one number (e.g.: 1) it returns "R$ 1,00" (currently returns "R$ 1") and when I enter 2 numbers (e.g.: 11) it returns "R$ 1,10" (currently returns "R$ 1,1) hence from these two conditions it can already start to run the way it is (ex: if you type "111" returns "R$ 1.11"; type "1111" > "R$ 11.11").

Ah, if you have a different function (that serves the same way), you can send it, but please send some example online to test via input (other than by "Alert" or "console", commands this same scheme that I sent) because I do not know how to call the function in other molds (type "Function (number) {...")

2 answers

4

The simplest solution would be to use the function toFixed.

Follow an example:

var numero = 1;

console.log(numero.toFixed(2));

Note that this function rounds the numbers in some cases. Take this example.

var numero = 1.567;
console.log(numero.toFixed(2));

3

Converting numbers to currency without framework

It is possible to convert Number to String currency native, using only function toLocaleString(). Behold:

(10.9).toLocaleString(); // "10,90"
(1002.5).toLocaleString("pt-BR"); // "1.002,50"
(5.55).toLocaleString("pt-BR", {
  // Ajustando casas decimais
  minimumFractionDigits: 2,  
  maximumFractionDigits: 2
});

The best part is that this way you avoid doing the classic tricks in using Math.abs() or Number.prototype.toFixed() javascript.

And another nice detail, this floating point bug insurance:

// Resultado bugado
0.1+0.2 // 0.30000000000000004
// Resultado sem bugs
(0.1+0.2).toLocaleString(); // "0.3"

Functional example for your code:

$( document ).ready(function() {
    $("#verifica").on('click', function(){
      var val = new Number ($(".valr-parc").val());
        alert((val).toLocaleString("pt-BR", {
           minimumFractionDigits: 2,  
           maximumFractionDigits: 2
        }));
    })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<input class="valr-parc" type="text" name="valr-parc" />
<button id="verifica">Verificar</button>

Fiddle

See more in Number.prototype.toLocaleString()

  • Man, I don’t know how to use it there (I tried and I couldn’t) :/ Send an example with equal input is in mine currently, then you send with the changes

Browser other questions tagged

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