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. oracle数据本机自动备份

    1.创建三个文件 exp.list  内容:oracle数据库的用户名和密码 name pwd exp.log   主要用于存储在自动备份数据库时的日志信息 exp.sh #!/bin/sh #找到数 ...

  2. Safari 前端开发调试 iOS 完美解决方案

    转http://www.2cto.com/kf/201403/283404.html afari 前端开发调试 iOS 完美解决方案 2014-03-05      0个评论    来源:Safari ...

  3. bk. 2014.12.1

    typedef void (*halKeyCback_t) (uint8 key, uint8 state) 表示定义halKeyCBack_T为指向函数的指针,该函数的特点是形参(uint8,uin ...

  4. 【Unity3D基础教程】给初学者看的Unity教程(一):GameObject,Compoent,Time,Input,Physics

    作者:王选易,出处:http://www.cnblogs.com/neverdie/  欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! Unity3D重要模块的类图 最近刚刚完成了一 ...

  5. Android学习四:数据库操作

    1前言 android中使用SQLite作为数据库,在进行相关的开发的时候不需要导入包.SQLite起符合SQL标准,也有自己的一些特性,是一个轻量级的数据库. 2代码 简单的数据库类封装 packa ...

  6. IIS-Server is too busy _解决方法

    httpRuntime Server Too Busy 修改方法:修改服务器.net配置“machine.config"文件,该文件位于Windows系统目录下,如“C:\WINDOWS \Micro ...

  7. oracle直通车6关于rman备份恢复数据文件,以及创建分区表的实验

    1.创建一张表,在表上创建一个索引,分别查询表,索引各自分配了多少个extents,多少个数据块以及总共占用空间的大小(bytes). 答:创建一张表t,为字段object_id创建索引t_objec ...

  8. [HTML] CSS3 文本效果

    CSS3 文本效果 CSS3中包含几个新的文本特征. 在本章中您将了解以下文本属性: text-shadow word-wrap 浏览器支持

  9. [IIS]IIS扫盲(三)

      IIS扫盲贴 --==[精品]==--   IIS扫盲贴     --==[精品]==-- 2000对应iis5.0  xp对应iis5.1  2003对应iis6.0        作者: II ...

  10. Linux下ejabberd安装配置

    1.下载Ejabberd安装包 wget http://www.process-one.net/downloads/ejabberd/2.1.13/ejabberd-2.1.13-linux-x86_ ...