Search code examples
javascriptjquerycurrency-formatting

JS: Calculating values that have dot and comma (i.e 1.000,00)


I have a script using jQuery which calculates currency. It works fine, but when I write a value greater than 999,99, it returns a value with error when I use a mask and convert the result to money format. How can I fix that?

UPDATED

$(document).ready(function() { 
    $('.money').mask("#.##0,00", {reverse: true});
});

accounting.settings = {
  currency: {
    symbol : "",   // default currency symbol is '$'
    format: "%s%v", // controls output: %s = symbol, %v = value/number (can be object: see below)
    decimal : ",",  // decimal point separator
    thousand: ".",  // thousands separator
    precision : 2   // decimal places
  },
  number: {
    precision : 0,  // default precision on numbers is 0
    thousand: ".",
    decimal : ","
  }
}


function calc(){
  var plano = parseFloat($("#regimento").val().replace(/,/g, '.'));
  var gasto = parseFloat($("#gasto_contabilidade").val().replace(/\./g, ',').replace(/,/g, '.'));
  var meses = parseInt( $("#meses").val());

  var resultado_gasto = parseFloat(gasto * meses);
  var resultado_plano = parseFloat(plano * meses);

  var result = resultado_gasto - resultado_plano;
  result = accounting.formatMoney(result);
  console.log(result)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.js"></script>

<script src="http://dubsolucoes.com.br/razonet/wp-content/themes/razonet_wp/assets/javascript/vendor/accounting.min.js"></script>



<select name="" id="regimento" class="select">
  <option value="19,90">MEI</option>
  <option value="85,90">SIMPLES SERVIÇO</option>
  <option value="199,90">SIMPLES COMÉRCIO E INDÚSTRIA</option>
</select>

<input id="gasto_contabilidade" type="text" class="money" value="100000">

<select id="meses" class="select">
  <option value="12">12</option>
  <option value="13">13</option>
</select>
<input type="button" onclick="calc()" value="Calculate">


Solution

  • FIX:

    I solve the problem removing the dot before calculate it

    var gasto = $("#gasto_contabilidade").val().replace(".", '');
    

    Thanks by helping guys!

    $(document).ready(function() { 
        $('.money').mask("#.##0,00", {reverse: true});
    });
    
    accounting.settings = {
      currency: {
        symbol : "",   // default currency symbol is '$'
        format: "%s%v", // controls output: %s = symbol, %v = value/number (can be object: see below)
        decimal : ",",  // decimal point separator
        thousand: ".",  // thousands separator
        precision : 2   // decimal places
      },
      number: {
        precision : 0,  // default precision on numbers is 0
        thousand: ".",
        decimal : ","
      }
    }
    
    
    function calc(){
      var gasto = $("#gasto_contabilidade").val().replace(".", ''); // FIX
      plano = parseFloat($("#regimento").val().replace(/,/g, '.'));
      gasto = parseFloat(gasto.replace(/\./g, ',').replace(/,/g, '.'));
      var meses = parseInt( $("#meses").val());
    
      var resultado_gasto = parseFloat(gasto * meses);
      var resultado_plano = parseFloat(plano * meses);
    
      var result = resultado_gasto - resultado_plano;
      result = accounting.formatMoney(result);
      console.log(result)
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.js"></script>
    
    <script src="http://dubsolucoes.com.br/razonet/wp-content/themes/razonet_wp/assets/javascript/vendor/accounting.min.js"></script>
    
    
    <script>
    
    
    
    </script>
    
    <select name="" id="regimento" class="select">
      <option value="19,90">MEI</option>
      <option value="85,90">SIMPLES SERVIÇO</option>
      <option value="199,90">SIMPLES COMÉRCIO E INDÚSTRIA</option>
    </select>
    
    <input id="gasto_contabilidade" type="text" class="money" value="100000">
    
    <select id="meses" class="select">
      <option value="12">12</option>
      <option value="13">13</option>
    </select>
    <input type="button" onclick="calc()" value="Calculate">