round是四舍五入,注意负数5是舍的,例如:Math.round(1.5)值是2,Math.round(-1.5)值是-1;floor就是直接去掉小数保留整数,即如果参数是正数则小数部分全舍,参数是负数则小数部分全入。 例如:Math.floor(2.6)的值是2,Math.floor(-2.1)的值是-3
round 返回最接近参数,意思也就是四舍五入。而 floor 则是返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数 返回值:double例如:Math.floor(1.123),那么则返回 1 Math.floor(-1.512),那么则返回-2乏意就是:取该数的小数位活动的最小值
int a;double b = 1.9,c=1.1;// round方法的参数,小数点后面的值 >=0.5,则返回整数部分+1,否则返回整数部分a = Math.round(b) // a = 2a = Math.round(c) // a = 1 // 如果想只要有小数则进1的话,比如做WEB的分页模式的时候,10条/页,现有62条记录,那么必须是7页,而不能是6页 则用Math.ceil()a = Math.ceil(b) // a = 2a = Math.ceil(c) // a = 2//如果想只取整,砍掉小数部分 则用强制造型法a = (int) b // a = 1a = (int)c // a = 1
round 就是四舍五入 math.round(-8.9) -9math.round(-8.1) -8math.round(8.9) 9math.round(8.1) 8