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

数値の小数点第2位を四捨五入するには、Math.round()を使います。
まず、Math.round()を呼び出します。
Math.round()の引数に、数値を10倍した値を指定します。
そして、Math.round()の結果を10で割ります。
const result = Math.round(number * 10) / 10
上記の割り算は、数値の小数点第2位を四捨五入した数値を返します。
使用例
var num = 1.521;
var num2 = 2.8861;
var num3 = 3.25749;
const result = Math.round(num * 10) / 10
const result2 = Math.round(num2 * 10) / 10
const result3 = Math.round(num3 * 10) / 10
console.log(result)
console.log(result2)
console.log(result3)
出力:
1.5
2.9
3.3
コメント