Java Math的 floor,round和ceil的总结

来源:互联网 发布:淘宝复古女装店铺 编辑:程序博客网 时间:2024/05/01 02:38
floor 向下取整 ceil  向上取整 round 则是4舍5入的计算,round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。 Math.floor(1.4)=1.0 Math.round(1.4)=1 Math.ceil(1.4)=2.0 Math.floor(1.5)=1.0 Math.round(1.5)=2 Math.ceil(1.5)=2.0 Math.floor(1.6)=1.0 Math.round(1.6)=2 Math.ceil(1.6)=2.0 Math.floor(-1.4)=-2.0 Math.round(-1.4)=-1 Math.ceil(-1.4)=-1.0 Math.floor(-1.5)=-2.0 Math.round(-1.5)=-1 Math.ceil(-1.5)=-1.0 Math.floor(-1.6)=-2.0 Math.round(-1.6)=-2 Math.ceil(-1.6)=-1.0 
备注:不管是java还是js,若是整数除以整数,小数部分将被舍弃。如下: 
1.丢弃小数部分,保留整数部分 
parseInt(5/2) 

2.向上取整,有小数就整数部分加1 

Math.ceil(5/2) 

3,四舍五入. 

Math.round(5/2) 

4,向下取整 

Math.floor(5/2) 


Math 对象的方法 

FF: Firefox, N: Netscape, IE: Internet Explorer 
方法 描述 FF N IE 
abs(x) 返回数的绝对值 1 2 3 
acos(x) 返回数的反余弦值 1 2 3 
asin(x) 返回数的反正弦值 1 2 3 
atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值 1 2 3 
atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间) 1 2 3 
ceil(x) 对一个数进行上舍入。 1 2 3 
cos(x) 返回数的余弦 1 2 3 
exp(x) 返回 e 的指数。 1 2 3 
floor(x) 对一个数进行下舍入。 1 2 3 
log(x) 返回数的自然对数(底为e) 1 2 3 
max(x,y) 返回 x 和 y 中的最高值 1 2 3 
min(x,y) 返回 x 和 y 中的最低值 1 2 3 
pow(x,y) 返回 x 的 y 次幂 1 2 3 
random() 返回 0 ~ 1 之间的随机数 1 2 3 
round(x) 把一个数四舍五入为最接近的整数 1 2 3 
sin(x) 返回数的正弦 1 2 3 
sqrt(x) 返回数的平方根 1 2 3 
tan(x) 返回一个角的正切 1 2 3 
toSource() 代表对象的源代码 1 4 - 
valueOf() 返回一个 Math 对象的原始值 
0 0
原创粉丝点击