[PR]
2025年08月24日 13時20分28秒
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
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
* 四捨五入
* 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