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. css transform skew变换

    两个参数,x-保持纵坐标不变,所有点旋转逆时针旋转x度,y-横坐标不变所有点顺时针旋转y度

  2. 2、C#入门第2课

    1.XML文件读取 XmlDocument xml; string path = "F:\\C#\\功2016.xml"; if (System.IO.File.Exists(pa ...

  3. eclipse导入github项目

    以jeesite为例, github上面的项目大都是基于git方式进行版本控制以及使用maven构建的项目. 1 使用时,需先用eclipse的以git方式从github上下载代码. 下载后得到mav ...

  4. Java的委托

    http://www.cnblogs.com/soojoe/archive/2012/04/12/2532304.html 委托模式是软件设计模式中的一项基本技巧.在委托模式中,有两个对象参与处理同一 ...

  5. WCF:没有终结点在侦听可以接受消息的*这通常是由于不正确的地址或者 SOAP操作导致的。

    没有终结点在侦听可以接受消息的 http://xx.com/WebService.svc. 这通常是由于不正确的地址或者 SOAP 操作导致的.如果存在此情况,请参见 InnerException 以 ...

  6. python学习-day20、装饰器【图片缺失可看】印象笔记博客备份

    前言: 装饰器用于装饰某些函数或者方法,或者类.可以在函数执行之前或者执行之后,执行一些自定义的操作. 1.定义:装饰器就是一个函数,为新定义的函数.把原函数嵌套到新函数里面.以后就可以在执行新函数的 ...

  7. android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )屏幕适配

    http://www.tuicool.com/articles/nuyMZb 1 Android手机目前常见的分辨率 1.1 手机常见分辨率: 4:3 VGA     640*480 (Video G ...

  8. coderforces719b

    题目大意:给定一个字符串,这个字符串中只有“r”和"b"组成,每次操作只能交换两个字符的位置或者将一个字符由"r"变"b"(或由" ...

  9. redis shell命令大全

    redis shell命令大全(转自http://blog.mkfree.com/posts/5105432f975ad0eb7d135964) 作者:oyhk   2013-1-28 3:11:35 ...

  10. NGUI之UIRoot

    原文:http://www.tasharen.com/forum/index.php?topic=6710.0 概述 UIRoot总是放在NGUI UI层级的最上层. 它用来使UI的缩放变得更容易.w ...