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

数値の小数点第3位を四捨五入するには、Math.round()を使います。
まず、Math.round()を呼び出します。
Math.round()の引数に、数値を100倍した値を指定します。
そして、Math.round()の結果を100で割ります。
const result = Math.round(number * 100) / 100
上記の割り算は、数値の小数点第3位を四捨五入した数値を返します。
使用例
var num = 1.521;
var num2 = 2.8861;
var num3 = 3.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)
出力:
1.52
2.89
3.26
コメント