忍者ブログ

JavaScriptで小数第n位を四捨五入

2016年11月03日 00時01分38秒
/*
 * 四捨五入
 * val:四捨五入する値
 * n:小数点以下の桁数
*/
function round(val, n) {
    var pow = Math.pow(10, n);
    return Math.round(val * pow) / pow;
}

round(1.456, 1) → 1.5
round(1.456, 2)) → 1.46

PR
Comment
  Vodafone絵文字 i-mode絵文字 Ezweb絵文字