Math.round(变量) (用于数字取整:四舍五入)
注:Math.round()以后类型是number
var str = "1.534";
var num = Math.round(str);
console.log(num); // number
console.log(typeof num); //控制台打印输出2
变量 . toFixed(保留位数) :可以指定四舍五入保留位数
注:toFixed以后类型是string
var num1 = 3.141592657;
num1 = num1.toFixed(4); // 把小数保留4位,四舍五入
console.log(num1);//打印3.1416
console.log(typeof num1); // string
本文介绍JavaScript中使用Math.round()进行数字四舍五入取整的方法,以及如何利用变量的toFixed()方法保留特定位数的小数并进行四舍五入。通过具体示例,展示了不同类型数字处理的实际应用。
1747

被折叠的 条评论
为什么被折叠?



