Arredondamento em javascript

1 resposta
S

Olá

Como faria um arredondamento do valor 30.655 para 30.66 com javascript?

Grato

1 Resposta

T

Faz tanto tempo que escrevi isto que nem sei se funciona direito.

/**
 * Internal rounding
 * @param x The to be rounded value
 * @param n The number of decimal digits
 * @return The rounded number.
 * @sample n = 3
 * 1/7 = 0.142857..., CALC_rnd(1/7, 3) = 0.143
 * -1/7 = -0.142857..., CALC_rnd(-1/7,3) = -0.143
 */
function CALC_rnd (x, n)
{
    if (n &lt 0 || n &gt 10) return x;
    var pow10 = Math.pow (10, n);
    var y = x * pow10;
    return Math.round (y) / pow10; 
}
Criado 22 de dezembro de 2006
Ultima resposta 22 de dez. de 2006
Respostas 1
Participantes 2