Math.round(),Math.ceil(),Math.floor()

Math.round() :round周围,求一个附近的 整数

小数点后第一位 < 5
正数:Math.round(10.48)       //  10
负数:Math.round(-10.45)     //  -10
小数点后第一位  > 5 
正数:Math.round(10.68)       //  11 
负数:Math.round(-10.68)      //  -11
小数点后第一位  === 5
正数:Math.round(11.5)         // 12
负数:Math.round(-11.5)        // -11
 
总结:正数情况 小数点后一位大于或等于5 全部往上的取值,小于5往下的取值
   负数情况 小数点后一位小于5 或等于5往上取值, 大于5往下取值
 
Math.ceil() :ceil天花板意思, 全部往大的取值
Math.ceil(11.46)  Math.ceil(11.68)   Math.ceil(11.5)        // 12
Math.ceil(-11.46) Math.ceil(-11.68)  Math.ceil(-11.5)     // -11
 
Math.floor():floor:“地板” 意思  全部往小的取值
Math.floor(11.46)            //11
Math.floor(11.68)            //11
Math.floor(11.5)              //11
 
Math.floor(-11.46)          // -12
Math.floor(-11.68)          // -12
Math.floor(-11.5)            // -12
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/musi03/p/10494146.html
今日推荐