Math ceil(),floor(),round()方法的使用
来源:互联网 发布:吉克隽逸 知乎 编辑:程序博客网 时间:2024/04/30 16:09
floor 返回不大于的最大整数
round 则是4舍5入的计算,入的时候是到大于它的整数(当-1.5时可见,四舍五入后得到的结果不是我们期待的,解决办法是先对他取绝对值,然后在用round方法)
round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。
ceil 则是不小于他的最小整数
看例子
测试程序如下:
- public class MyTest {
- public static void main(String[] args) {
- double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };
- for (double num : nums) {
- test(num);
- }
- }
- private static void test(double num) {
- System.out.println("Math.floor(" + num + ")=" + Math.floor(num));
- System.out.println("Math.round(" + num + ")=" + Math.round(num));
- System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
- }
- }
运行结果
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
- Math ceil(),floor(),round()方法的使用
- Java Math的floor、ceil、round方法
- Android中Math类Math.floor()、Math.round()及Math.ceil()等方法的使用
- Java中Math类Math.floor()、Math.round()及Math.ceil()等方法的使用
- Math ceil()、floor()、round()方法
- Math ceil()、floor()、round()方法
- Math ceil()、floor()、round()方法
- Math ceil()、floor()、round()方法
- java中Math方法的ceil,floor,round方法
- Java Math类的三个方法ceil,floor,round
- java Math的round,floor,ceil,IEEEremainder 方法总结
- Java Math的floor、ceil、rint及round方法
- Math.round ceil floor
- Java Math的 floor,round和ceil的使用
- JavaScript Math对象 ceil()、floor()、round()方法
- js Math.abs,ceil,floor,round,toFixed函数的使用
- Math ceil()、floor()、round()、random()使用
- Java Math的floor,round,ceil使用方法
- nginx 基础-rewirte指令
- window.open
- spring ApplicationContext#getBean 方法
- MFC之Ribbon
- MFC之Dialog
- Math ceil(),floor(),round()方法的使用
- 如何实现类的成员函数作为回调函数
- 韩顺平玩转oracle之pl/sql听课笔记《三》
- Android软件开发之PreferenceActivity中的组件
- 博客技巧
- quake3源码大致结构
- Hibernate工作流程
- 磁盘移臂调度算法 简介
- 实现从左边滑动出来的抽屉