转自 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. hibernate得知——Set设置配置

    Set设置配置 创建数据表:表关系的员工有多重身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR ...

  2. Jedis Client的使用以及序列化

    JedisPool pool = new JedisPool(poolConfig, IP, PORT, timeout); public String set(String key,String v ...

  3. .net数据根据字段进行分类(linq语句)

    var items = List<实体>; var models = items.GroupBy(r => r.分类字段).ToDictionary(d => d.Key, d ...

  4. Android应用Activity、Dialog、PopWindow、Toast窗体加入机制及源代码分析

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重劳动成果] 1 背景 之所以写这一篇博客的原因是由于之前有写过一篇<Android应用setCont ...

  5. php-GD库函数(三)

    <?php //imagefilledellipse — 画一椭圆并填充 /*bool imagefilledellipse ( resource $image , int $cx , int ...

  6. nyoj 228 士兵杀死(五岁以下儿童)【树状数组】

    分析:这个问题问的是,因为它是一个单独的更新.因此,让我们更新,然后在c[i]表现为1~i之间,还原之后看起来像一个. #include <cstdio> #include <cst ...

  7. HSQL

    Whenever I connect to HSQLDB from my application deployed on eclipse Juno, it throws an exception as ...

  8. sql查询 数据库 表 字段 等

    1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...

  9. Flex强制类型转换错误

    1.错误描写叙述 TypeError: Error #1034: 强制转换类型失败:无法将 HoleDetailInnerClass0@c2cccf1 转换为 mx.controls.listClas ...

  10. DP:树DP

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...