转自 http://blog.csdn.net/foart/article/details/4295645

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。(正数小数点后大于5则进位;负数小数点后小于以及等于5都舍去,大于5的则进位)

ceil 则是不小于他的最小整数

看例子

  Math.floor Math.round Math.ceil
1.4 1
1.5 1
1.6 1
-1.4 -2 -1  -1 
-1.5 -2 -1  -1 
-1.6 -2 -2  -1 
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

Java Math的floor,round,ceil函数小结的更多相关文章

  1. Java Math floor round ceil 函数

    public final class Math extends Object public static double floor(double a) public static long round ...

  2. JAVA除法保留小数点后两位的两种方法 Java Math的 floor,round和ceil的总结

    floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下 ...

  3. Java Math的 floor,round和ceil的总结

    floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下 ...

  4. Java Math的 floor,round和ceil

    floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数 round方法,它表示"四舍五入",算法为Math.floor(x+0.5),即将原来的 ...

  5. Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

    Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 FLOOR——对给定的数字取整数位SQL> select floor(2345.67) from dua ...

  6. 问题:oracle floor;结果:Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

    Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 (2011-04-06 16:10:35) 转载▼ 标签: 谈 分类: 渐行渐远 FLOOR——对给定的数字取 ...

  7. floor() 和 ceil()函数

    在C语言的库函数中,floor函数的语法例如以下: #include <math.h> double floor( double arg ); 功能: 函数返回參数不大于arg的最大整数. ...

  8. js中Math之random,round,ceil,floor的用法总结

    1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) 2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分(返回小于等于n的最大整数). ...

  9. Math中floor,round和ceil的区别

    floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数(当-1.5时可见,四舍五入后得到的结果不是我们期待的,解决办法是先对他取绝对值,然后在用round方法) r ...

随机推荐

  1. 【DRP】删除递归树的操作

    正如图呈现的树结构.本文从任意节点删除树形结构.提供解决方案 图中,不包括其他结点的是叶子结点.包括其他结点的是父结点,即不是叶子结点. 一 本文的知识点: (1)递归调用: 由于待删除的结点的层次是 ...

  2. OpenStack安装与配置2

    第二部分 OpenStack安装与配置 一.引言   本章内容讲解如何在3台物理机上搭建最小化云平台,这3台机器分为称为Server1.Server2和Client1,之后的各章也是如此.Server ...

  3. 在VS2012中使用GitHub

    注册GitHub账号(DeanZhouLin) https://github.com/ 向GitHub中添加一个仓库(Test) *创建完成后,记录该仓库的地址:https://github.com/ ...

  4. WPF界面设计技巧(4)—自定义列表项样式

    原文:WPF界面设计技巧(4)-自定义列表项样式 有前面修改按钮样式的基础,我们可以尝试来定制一个即好看又好用的 ListBox ,今天先来讲“好看”部分. 打开 Microsoft Visual S ...

  5. jsoup方法string转document

    //Document doc2 = Jsoup.parseBodyFragment(element.text());                    //String FieldName=doc ...

  6. 六:Java之集合

    集合包括的内容非常多,我发现一个非常好的博客,感觉自己都没有写的必要了! 链接献上  Java集合

  7. mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry &#39;xxx&#39; for key &#39;PRIMARY&#39;

    mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry 'xxx' for key 'PRIMARY' 解决方法: 在my.cnf的[mysql ...

  8. C#实现仿QQ震动

    前提:新建winForm窗体应用程序,放置一个Button,设置按钮的单击事件 ; i < ; i++) { Point p = this.FindForm().Location; ,p.Y+) ...

  9. sscanf,sscanf_s及其相关使用方法

    #include<stdio.h> 定义函数 int sscanf (const char *str,const char * format,........); 函数说明   sscan ...

  10. Windows Phone开发(37):动画之ColorAnimation

    原文:Windows Phone开发(37):动画之ColorAnimation 上一节中我们讨论了用double值进行动画处理,我们知道动画是有很多种的,今天,我向大家继续介绍一个动画类--Colo ...