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. 正则表达式常用用法汇总 __西科大C语言

    正则表达式,又称正规表示法.常规表示法.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式使用单个字符串来描述.匹配一系列 ...

  2. SQL数据库的十条命令

    --(1)查询每个总学时数 select GradeId,SUM(classHour) from subject group by GradeId order by(SUM(classHour)) - ...

  3. Hadoop学习14--Hadoop之一点点理解yarn

    yarn是一个分布式的资源管理系统. 它诞生的原因是原来的MapReduce框架的一些不足: 1.JobTracker单点故障隐患 2.JobTracker承担的任务太多,维护Job状态,Job的ta ...

  4. 【AT91SAM3S】ADC中断方式采集数据

    板子依旧是英倍特的EK-SAM3S.ADC部分的原理图如下: PB1是一个复用引脚,在这里被用作AD功能,对应芯片上的AD5.即,使用片内ADC的5通道测VR1上2号引脚的电压. 实验采用了SysTi ...

  5. Python 列表

    python 列表 列表的特点 1.列表是一种可变的数据类型,这点是跟元组有区别的 2.列表中的值是有序的,并且可存放重复的值,这点跟set有区别的 3.python中的列表类似于其它语言中的数组 4 ...

  6. (Design Pattern) Singleton.

    Role: The purpose of the Singleton pattern is to ensure that there is only one instance of a class, ...

  7. 关于华为x2中的外置SDCard的使用

    一.前要 记录一下最近在开发生产过程中遇到的一个小问题.在使用x2的华为7寸平板时,由于需要大内存去录制视频,所以就使用X2自带的SDCard口,插入1个128G的SDCard卡.但是Android ...

  8. UVA 10003 切木棍(普通DP)

    切木棍 紫书P278 算是简单的dp了吧,当然,这是看完别人题解后的想法,呵呵,我仍然是想了半小时,没思路,啥时候能自个整个dp啊!!→_→ dp的时候,输入数组必须从1开始,一定要注意状态的设计,和 ...

  9. LINUX下动态库及版本号控制

    针对同一动态组件的不同版本链接和加载. 一.概念                  DLL HELL字面意思是DLL"灾难",是由于com组件(动态库)升级引起的程序不能运行的情况 ...

  10. bootstrap-图文混排 media

    <!-- media 图文混排 media-left(right) 图片的区域 在左边显示(右边) media-body 内容区域 media-heading 内容区域里的标题 media-mi ...