转自 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. 判断 iPhone 是否已插入 SIM 卡的方法

    判断 iPhone 是否插入了 SIM 卡,可以参考苹果官网的 systemconfigure framework 教程,将下面的代码复制到头文件 extern NSString* const kCT ...

  2. ASP.NET——验证码的制作

            我们在登陆站点,发表博客或者提交评论的时候,常常会遇到填写验证码这一项,当时感觉挺奇妙的样子,最终在牛腩新闻公布系统里接触到了,在这里小小的总结下.         用到的东东有三个: ...

  3. 谷歌Web中国开发手册:1目的&amp;夹

    原版的:https://developers.google.com/web/fundamentals/getting-started/your-first-multi-screen-site/ 该网站 ...

  4. SQL Server中的查询

          本博文简介一下SQL Server中经常使用的几类查询及相关使用的方法.       一.ExecuteScalar方法获取单一值       ExecuteScalar方法是SqlCom ...

  5. listary文件查找程序下载和使用

    资源:PHP开发学习门户网站 地址:http://bbs.phpthinking.com/forum.php?mod=viewthread&tid=173 Listary 是一款有用的国产Wi ...

  6. 关于扩展IP地址空间的几个方案的探讨

    摘  要:在IP地址紧缺的背景下,IPv6.NAPT.SuIP几种解决方案应运而生.分析.比较几种方案后,可知SuIP是最佳方案. 关键词:IPv6:NAPT:SuIP:IP地址空间扩展 The In ...

  7. 变化App.config其中值,并保存

    using System; using System.Collections.Generic; using System.Configuration; using System.IO; using S ...

  8. Asp.net MVC4之 一个简单的小例子

    练习: 新建一个mvc项目 要求: 有3个视图  Login Index Details 目的:感受一下MVC与传统WebForm的差异性 WebForm的请求模型 MVC请求模型 传统WebForm ...

  9. WPF 3D:使用GeometryModel3D的BackMaterial

    原文 WPF 3D:使用GeometryModel3D的BackMaterial 使用BackMaterial,我们可以定义3D物体的内部材质(或者说是背面),比如,我们定义一个四方体容器,外面现实的 ...

  10. Python re正则表达式

    参考: 博客园<Python正則表達式指南> 博客园<Python之re模块> 在此感谢博主的无私奉献,本文主要介绍python中re模块的几个简单使用方法.具体的使用方法请參 ...