java 中取整操作提供了四种方法:分别是:

public static double ceil(double a)//向上取整

 public static double floor(double a)//向下取整
 public static long round(double a)//四舍五入取整
 public static double rint(double a)//最近取整
 
 

第一种:ceil是天花板的意思,表示向上取整。   测试:

System.out.println(Math.ceil(1.01));
System.out.println(Math.ceil(-1.01));
System.out.println(Math.ceil(1.5));
System.out.println(Math.ceil(-1.5));
 
 

输出结果:

2.0
-1.0
2.0
-1.0
 
 

第二种:floor是地板的意思,表示向下取整。   测试:

System.out.println(Math.floor(1.01));

System.out.println(Math.floor(-1.01));
System.out.println(Math.floor(1.5));
System.out.println(Math.floor(-1.5));
 
 

输出:

1.0

-2.0
1.0
-2.0
 
 

第三种:round执行的就是数学上的四舍五入运行。   查看它源码可知其与floor方法的关系:

public static long round(double a) {
return (long)floor(a + 0.5d);
    }
 
 

测试:

System.out.println(Math.round(-1.5));
System.out.println(Math.round(1.5));
结果:
-1
2
 
 

第四种:最有意思的,返回最接近参数的整数,如果有2个数同样接近,则返回偶数的那个。它有两个特殊的情况:

1)如果参数本身是整数,则返回本身。

2)如果不是数字或无穷大或正负0,则结果为其本身。

Returns the double value that is closest in value to the argument and is equal to a mathematical integer. If two double values that are mathematical integers are equally close, the result is the integer value that is even. Special cases:  
If the argument value is already equal to a mathematical integer, then the result is the same as the argument.

If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.

Parameters:  a a double value.

Returns:  the closest floating-point value to a that is equal to a mathematical integer. 
测试:

System.out.println(Math.rint(-1.5));
System.out.println(Math.rint(1.5));
System.out.println(Math.rint(-2.5));
System.out.println(Math.rint(2.5));
结果:
-2.0
2.0
-2.0
2.0
 
 

java的四种取整方法的更多相关文章

  1. C语言的几种取整方法

    C语言的几种取整方法 来源:http://blog.sina.com.cn/s/blog_4c0cb1c001013ha9.html 1.直接赋值给整数变量.如: int i = 2.5; 或 i = ...

  2. Java中四种复制数组的方法

    JAVA语言的下面几种数组复制方法中,哪个效率最高? B.效率:System.arraycopy > clone > Arrays.copyOf > for循环 1.System.a ...

  3. 转载:Java的三种取整办法

    转载地址:https://blog.csdn.net/maple_fix/article/details/78656152 方法一:向上取整Math.ceil();举例:Math.ceil(11.4) ...

  4. c# 三种取整方法 向上取整 向下取整 四舍五入

    Math.Round:四舍六入五取整 Math.Ceiling:向上取整,只要有小数都加1 Math.Floor:向下取整,总是舍去小数

  5. C语言取整方法总结

    C语言有下面几种取整方法: 1.   直接赋值给整数变量     int i = 3.5; 或 i = (int) 3.5; 这样的方法採用的是舍去小数部分. 2.整数除法运算符' / '取整 ' / ...

  6. JavaScript四种数值取整方法

    一.Math.trunc() 1.定义 Math.trunc()方法去除数字的小数部分,保留整数部分. 2.语法 Math.trunc(value) 3.示例 console.log(Math.tru ...

  7. Java中四种引用:强、软、弱、虚引用

    这篇文章非常棒:http://alinazh.blog.51cto.com/5459270/1276173 Java中四种引用:强.软.弱.虚引用 1.1.强引用当我们使用new 这个关键字创建对象时 ...

  8. Java基础:Java的四种引用

    在Java基础:java虚拟机(JVM)中,我们提到了Java的四种引用.包括:强引用,软引用,弱引用,虚引用.这篇博客将详细的讲解一下这四种引用. 1. 强引用 2. 软引用 3. 弱引用 4. 虚 ...

  9. Java的四种内部类

    Java的四种内部类包括如下: 成员内部类 静态内部类 局部内部类 匿名内部类 成员内部类: 定义在另一个类(外部类)的内部,而且与成员方法和属性平级叫成员内部类,......相当于外部类的非静态方法 ...

随机推荐

  1. (转)注意力机制(Attention Mechanism)在自然语言处理中的应用

    注意力机制(Attention Mechanism)在自然语言处理中的应用 本文转自:http://www.cnblogs.com/robert-dlut/p/5952032.html  近年来,深度 ...

  2. lua学习记录

    1.八种数据类型:number,string,boolean,nil,function,table,协程,自定义类型 空字符串和数字0是真,false和nil为假2.lua是动态语言,每个变量携带自己 ...

  3. IOS开发-第三方SDWebImage下载网络图片的使用

    从网络上请求图片时,没有使用第三方的话,下载会很慢,而且堵塞线程,还要自己处理多线程问题,效果还非常不明显,使用了SDWebImage这个第三方类库之后,下载图片就变的容易多了. SDWebImage ...

  4. Spring MVC小结

    Spring MVC项目搭建 添加依赖 (省略) Spring MVC配置类 @Configuration @EnableWebMvc @ComponentScan("com.sjx.spr ...

  5. gc之四--Minor GC、Major GC和Full GC之间的区别

    针对HotSpot VM的实现,它里面的GC其实准确分类只有两大种: Partial GC:并不收集整个GC堆的模式 Young GC:只收集young gen的GC Old GC:只收集old ge ...

  6. javascript内建对象

    内建对象等价于内建构造器内建对象大致分为三类:数据封装类对象--Object.Array.Boolean.Number和String工具类对象--Math.Date.RegExp等用于提供遍历的对象错 ...

  7. springboot教程

    http://www.cnblogs.com/java-zhao/tag/spring-boot/ http://blog.csdn.net/liaokailin/article/category/5 ...

  8. South - 在 Django 中 Migrate Database

    Web 开发避免不了经常修改表结构,手工修改表结构不仅容易出错,而且涉及到多人协作开发时,这么土的做法很不经济. Django 的第三方 app South 就是专门做数据库表结构自动迁移的.Jaco ...

  9. 用wireshark抓包分析TCP三次握手、四次挥手以及TCP实现可靠传输的机制

    关于TCP三次握手和四次挥手大家都在<计算机网络>课程里学过,还记得当时高超老师耐心地讲解.大学里我遇到的最好的老师大概就是这位了,虽然他只给我讲过<java程序设计>和< ...

  10. Action<T1, T2>委托

    封装包含两个参数的方法委托,没有返回值. 语法 public delegate void Action<in T1, in T2>( T1 arg1, T2 arg2 ) 类型参数 in ...