どうも、ちょげ(@chogetarou)です。
数値の10の位を四捨五入する方法を紹介します。
方法

数値の10の位を四捨五入するには、Math.round()を使います。
まず、Math.round()を呼び出します。
Math.round()の引数に、数値を100で割った値を指定します。
そして、Math.round()の結果を100倍します。
const result = Math.round(number / 100) * 100;
上記の掛け算は、数値の10の位を四捨五入した数値を返します。
使用例
function roundUp(num, digits) {
const n = Math.pow(10, digits - 1);
return Math.ceil(num * n) / n;
}
var num = 152.1;
var num2 = 1048.861;
var num3 = 1213.25749;
const result = Math.round(num / 100) * 100;
const result2 = Math.round(num2 / 100) * 100;
const result3 = Math.round(num3 / 100) * 100;
console.log(result);
console.log(result2);
console.log(result3);
出力:
200
1000
1200
コメント