java floor,ceil和round方法


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

举例:    

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类型的,返回的是不小于它的最小整数

举例:

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 类型的,返回的是四舍五入或四舍六入后的整数

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

举例:

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM