美文网首页
Math round、ceil、floor方法

Math round、ceil、floor方法

作者: _woodman_ | 来源:发表于2020-04-30 15:05 被阅读0次
1、Math.round() 这个方法是求一个附近的整数(四舍五入)
//小数点后一位大于5
正数:Math.round(14.67); //15
负数:Math.round(-14.67); //-15

//小数点后一位小于5
正数:Math.round(14.47); //14
负数:Math.round(-14.47); //-14

//小数点后一位等于5
正数:Math.round(14.57); //15
负数:Math.round(-14.57); //-14

2、Math.ceil() 函数返回大于或等于一个给定数字的最小整数。
console.log(Math.ceil(.45));
// 1

console.log(Math.ceil(4));
// 4

console.log(Math.ceil(7.004));
//  8

console.log(Math.ceil(-7.004));
// -7

3、Math.floor() 返回小于或等于一个给定数字的最大整数。
Math.floor( 45.95); 
// 45 
Math.floor( 45.05); 
// 45 
Math.floor( 4 ); 
// 4 
Math.floor(-45.05); 
// -46 
Math.floor(-45.95); 
// -46

相关文章

网友评论

      本文标题:Math round、ceil、floor方法

      本文链接:https://www.haomeiwen.com/subject/rrnpfctx.html