java floor,ceil和round方法

Math.floor():返回值是double类型的,返回的是不大于它的最大整数spa

举例:    code

1 double x = Math.floor(5.8);
2 System.out.println(x);    //输出结果:5.0     
3 double x = Math.floor(-2.5);
4 System.out.println(x);   //输出结果:-3.0

 

Math.ceil():返回值是double类型的,返回的是不小于它的最小整数blog

举例:class

1 double x = Math.ceil(5.8);
2 System.out.println(x);       //输出结果:6.0
3 double x = Math.ceil(-2.5);
4 System.out.println(x);       //输出结果:-2.0

 

Math.round():返回值是 int/long 类型的,返回的是四舍五入或四舍六入后的整数di

      (或者理解为Math.floor(x+0.5):在原来的数上+0.5再向下取整)co

举例:

1 int x = Math.round(1.6);    
2 System.out.println(x);        //输出结果:2
3 int x = Math.round(1.3);    
4 System.out.println(x);        //输出结果:1
5 
6 int x = Math.round(-1.6);    
7 System.out.println(x);        //输出结果:-2
8 int x = Math.round(-1.5);    
9 System.out.println(x);        //输出结果:-1
相关文章
相关标签/搜索