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. linux iftop流量查看工具的安装与使用

    1.安装依赖包yum install flex byacc  libpcap ncurses ncurses-devel libpcap-devel 2.下载iftop wget http://www ...

  2. popToViewController和dismissviewcontroller的用法

    如果我们有ABC三个controller 1.使用present从A到B.再present到C.如果我们想从C直接回到A的话.直接使用 self  dismissViewControllerAnima ...

  3. Yahoo Pure 中文参考手册

    Pure 是来自雅虎的 CSS 框架,使用 Normalize.CSS 无需任何 JavaScript 代码.框架基于响应式设计,提供多种样式的组件,包括表格.表单.按钮.表.导航等.标识使用非常简单 ...

  4. jquery easy ui combox

    $(document).ready(function() { $.ajax({ type: 'POST', dataType: "json", url:'menu/getAll', ...

  5. [转]配置mysql允许远程连接的方法

    配置mysql允许远程连接的方法 vim /etc/my.cnf注释这一行:bind-address=127.0.0.1 ==> #bind-address=127.0.0.1保存退出.mysq ...

  6. 调Windows 7的图片浏览器查看图片

    public static void viewPicFromWindows(string pPicPath) { if (pPicPath!="" && Syste ...

  7. ASP.NET中进行消息处理(MSMQ) 一(转)

    MSMQ是微软消息队列的英文缩写.那么什么是消息队列?这些介绍网上一大片这里就不多说了.本文对于大虾级的人物来说这只是小玩意而已,对于初学者来说这文章还是有一定的帮助,希望路过的大虾们别笑话我班门弄斧 ...

  8. Capture a Screen Shot

    using System; using System.Runtime.InteropServices; using System.Drawing; using System.Drawing.Imagi ...

  9. python_day2

    一.字符串的基本使用 #!/usr/bin/env python #!-*- coding:utf-8 -*- #!/usr/bin/env python  指定解释器为python abc='hel ...

  10. Embed dll Files Within an exe (C# WinForms)—Winform 集成零散dll进exe的方法

    A while back I was working on a small C# WinForms application in Visual Studio 2008. For the sake of ...