Olá
Como faria um arredondamento do valor 30.655 para 30.66 com javascript?
Grato
Olá
Como faria um arredondamento do valor 30.655 para 30.66 com javascript?
Grato
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 < 0 || n > 10) return x;
var pow10 = Math.pow (10, n);
var y = x * pow10;
return Math.round (y) / pow10;
}