java的四种取整方法
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.02.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.01.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));结果:-12|
|
|
第四种:最有意思的,返回最接近参数的整数,如果有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.02.0-2.02.0|
|
|
java的四种取整方法的更多相关文章
- C语言的几种取整方法
C语言的几种取整方法 来源:http://blog.sina.com.cn/s/blog_4c0cb1c001013ha9.html 1.直接赋值给整数变量.如: int i = 2.5; 或 i = ...
- Java中四种复制数组的方法
JAVA语言的下面几种数组复制方法中,哪个效率最高? B.效率:System.arraycopy > clone > Arrays.copyOf > for循环 1.System.a ...
- 转载:Java的三种取整办法
转载地址:https://blog.csdn.net/maple_fix/article/details/78656152 方法一:向上取整Math.ceil();举例:Math.ceil(11.4) ...
- c# 三种取整方法 向上取整 向下取整 四舍五入
Math.Round:四舍六入五取整 Math.Ceiling:向上取整,只要有小数都加1 Math.Floor:向下取整,总是舍去小数
- C语言取整方法总结
C语言有下面几种取整方法: 1. 直接赋值给整数变量 int i = 3.5; 或 i = (int) 3.5; 这样的方法採用的是舍去小数部分. 2.整数除法运算符' / '取整 ' / ...
- JavaScript四种数值取整方法
一.Math.trunc() 1.定义 Math.trunc()方法去除数字的小数部分,保留整数部分. 2.语法 Math.trunc(value) 3.示例 console.log(Math.tru ...
- Java中四种引用:强、软、弱、虚引用
这篇文章非常棒:http://alinazh.blog.51cto.com/5459270/1276173 Java中四种引用:强.软.弱.虚引用 1.1.强引用当我们使用new 这个关键字创建对象时 ...
- Java基础:Java的四种引用
在Java基础:java虚拟机(JVM)中,我们提到了Java的四种引用.包括:强引用,软引用,弱引用,虚引用.这篇博客将详细的讲解一下这四种引用. 1. 强引用 2. 软引用 3. 弱引用 4. 虚 ...
- Java的四种内部类
Java的四种内部类包括如下: 成员内部类 静态内部类 局部内部类 匿名内部类 成员内部类: 定义在另一个类(外部类)的内部,而且与成员方法和属性平级叫成员内部类,......相当于外部类的非静态方法 ...
随机推荐
- 初识Winform , 还好没喜欢上控制台
虽然没听的太懂, 不过还是写点东西吧. 我呢, 就跟着这本书写了个学生管理系统 前面刚会了SQLserver, 所以这个学生管理系统需要连上数据库, 毕竟学了不用天诛地灭 既然需要连接数据库, 就要用 ...
- Oracle学习系列4
Oracle学习系列4 ************************************************************************************ 数据库 ...
- github 如何合并不同分支
From: http://stackoverflow.com/questions/1123344/merging-between-forks-in-github 1. 添加remote origina ...
- phpunit4.1的干净测试
一般而言,写测试时需要加载一些文件来进行自动加载 但在phpunit4.1中只要其中一个测试文件加载了,其他测试文件就不需要再加载
- 我终于搞清楚为什么谷歌地图获取到的联通3G基站与大家手头的基站表不同了
我终于搞清楚这个问题了,大家使用谷歌地图手机版.MobileTrack以及网优用的FieldTest获取到的WCDMA基站Cellid为什么不是大家手头的CellTrack91或基站表里的数字了... ...
- Durid(一): 原理架构
Durid是在2013年底开源出来的,当前最新版本0.9.2, 主要解决的是对实时数据以及较近时间的历史数据的多维查询提供高并发(多用户),低延时,高可靠性的问题.对比Druid与其他解决方案,Kyl ...
- python---生成随机密码
#简短地生成随机密码,包括大小写字母.数字,可以指定密码长度 from random import choice import string #python3中为string.ascii_letter ...
- PHP入门part2
PHP的数据类型 php数据类型分为三大类, 标量类型:整型.浮点型.布尔型和字符串型 复合类型:数组型和对象(object) 特殊类型:空类型和资源型 !资源型以后会讲 整形(int)就是整数的数值 ...
- 关于jsp中response.sendRedirect显示错误
今天在jsp中作判断时,当不同条件时利用response.sendRedirect(“url”)来转向不同的页面,首先是判断验证码,当错误时就转向错误页面:当正确时,才进行用户名和密码的判断,同样也r ...
- OAF_文件系列3_实现OAF多行表中附件功能AttachmentImage(案例)
20150727 Created By BaoXinjian