How to limit the number of decimals to an input?

0

The total is generated multiplicanco amount x price in the following function

      function calcularMult(con){
      $("#resultado_" + con).val($("#nn_" + con).val() * $("#mm_" + con).val());
      var sum1 = 0;
      $("input[id^='resultado_']").each(function() {
        sum1 += Number($(this).val());
       });  
      sum1.toFixed(2);
      var sum=sum1;         
      $("#total").val(sum)
      }

<button type="button" name="crea" id="+" onClick="cargaProd();" class="btn btn-primary" )>
                  Nueva partida
                  </button>
                  <button type="button" name="envia" id="-" onClick="eliminar()" class="btn btn-danger")>
                  Eliminar Partida
                  </button>
                  <input type="button" name="process" class="btn btn-primary" onclick="pregunta()" value="<?php echo __("Terminar Orden de Compra"); ?>" >





                  <script type="text/javascript">
                    var con = 1;
                     var can = '<?php echo $quantity;?>'-1;


                    function cargaProd()
                      {
var linea = "<tr id='"+con+"'><td>"+con+"</td><td><input type='text' name='articulo["+can+"]' placeholder=' Articulo' class='span12 inputbox'/></td><td><input name='can["+can+"] ' id='nn_"+con+"' type='text' class='span6 inputbox' size='15' value='0' onkeyup='calcularMult("+con+")'></td><td><select name='medida["+can+"]' id='medida' class='selectpicker span9' ><option value=''></option><?php foreach ($resultado as $key => $value) {?><option value='<?php echo $value["Unidad"];  ?>'><?php echo $value["Unidad"];  ?></option><?php  } ?></select></td><td><input name='dp["+can+"]' id='mm_"+con+"' type='text' class='span6 inputbox' size='15' value='0' onkeyup='calcularMult("+con+")' ></td><td><input  name='to["+can+"]' id='resultado_"+con+"' type='number' step='0.01' class='span6 inputbox' size='15' ></td></tr>";

$('#listaArticulos').append(linea);
con = con+1
can = can+1
$("#quantity").val(can);
}
function eliminar(){
  //Determinamos Cuantas filas posee la lista
  var filas = $("#listaArticulos tr").length;

  //Eliminamos la última
  $("tr[id="+filas+"]").remove();
  //Si el contador no es igual a 1, le restamos la fila eliminada
  if(con>1){
    con = con-1
    can = can-1
    $("#quantity").val(can);

  }

}
    
asked by Henry 95 12.12.2018 в 19:02
source

2 answers

0

Your problem is simple.

function calcularMult(con){
  $("#resultado_" + con).val($("#nn_" + con).val() * $("#mm_" + con).val());
  var sum1 = 0;
  $("input[id^='resultado_']").each(function() {
    sum1 += Number($(this).val());
   });  
  //Cambiar
  var sum=sum1.toFixed(2);        
  $("#total").val(sum)
}

You are not assigning sum1.toFixed(2); to anything, so the result of this value remains in the air, you can do it as I put it in the previous code or simply;

  sum1 = sum1.toFixed(2);
  var sum=sum1; 

I hope it has helped you.

By the way, if the input is of type number, it is better to cast the variable;

  sum1 = Number(sum1.toFixed(2));
  var sum=sum1;
    
answered by 12.12.2018 / 20:26
source
0

UPDATE:

It's true that Firefox does not work. I'll put a little script to work for you there too:

$('#total').keyup(function(){
  var total = $(this).val();
  var separador;

  if(total.indexOf(',') != -1){
    separador = ',';
  }
  else if(total.indexOf('.') != -1){
    separador = '.';
  }

  if(separador){
    var decimales = total.split(separador);
    if(decimales[1].length>2){
      decimales[1] = decimales[1].substr(0, 2);
    }
    total = decimales.join(separador);
  }

  $(this).val(total);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="total" type="text">
    
answered by 12.12.2018 в 19:10