How to move the decimal point to the left n spaces with Jquery or JS?

3

I have the following code:

 $('#txtFederalCantidad').mask('00000000', { reverse: true });
                    valorFederal = $('#txtFederalCantidad').val(); 
                    $('#txtMonto').mask('00000000', { reverse: true });
                    var Monto = $('#txtMonto').val();
                    var Total = (valorFederal / Monto) * 100;
                    var TotalMostrar = Total.toFixed(2);
                    $('#txtFederalCantidad').mask('00.00', { reverse: true });         
                    $('#txtFederalCantidad').val(TotalMostrar); 

Suppose that the variable "TotalShow" results in 1658.99, How can I go through the decimal point so that it appears as 16.5899?

    
asked by Oscar Navarro 19.07.2017 в 17:16
source

1 answer

4

To run the decimal point, just multiply or divide by a multiple of 10.

  • To run the point to the right you multiply.
  • To break the point to the left you divide.
  • Assuming TotalShow is 1658.99

    Point to the left

    var TotalMostrar = 1658.99;
    TotalMostrar /= 100;
    //TotalMostrar ahora es igual a 16.5899
    

    Point to the right

    var TotalMostrar = 1658.99;
    TotalMostrar *= 10;
    //TotalMostrar ahora es igual a 16589.9
    

    Now, to determine the amount of spaces, as you say in the question, what you should do is raise 10 to n. For this you can use Math.pow() (more information here )

    var n = 2;
    var exponente = Math.pow(10, n);
    var TotalMostrar = 1658.99;
    TotalMostrar /= exponente;
    
        
    answered by 19.07.2017 / 17:23
    source