Java Math.round()函数小结
| Math类中提供了三个与取整有关的方法:ceil,floor,round,这些方法的作用于它们的英文名称的含义相对应,例如:ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3)的结果为12,Math.ceil(-11.6)的结果为-11;floor的英文是地板,该方法就表示向下取整,Math.floor(11.6)的结果是11,Math.floor(-11.4)的结果-12;最难掌握的是round方法,他表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果是12,Math.round(-11.5)的结果为-11.Math.round( )符合这样的规律:小数点后大于5全部加,等于5正数加,小于5全不加。 |
(1)public static long round(double a)
returns the closest long to the argument. the result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. in other words, the result is equal to the value of the expression:
(long)math.floor(a + 0.5d)
(2)public static double floor(double a)
returns the largest(closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.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 value.
returns:
the smallest (closest to negative infinity) floating-point value that is not less than the argument and is equal to a mathematical integer.
//import java.math.*;
public class RoundTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Math.round():Java中的四舍五入函数
System.out.println("Case1:小数点后第一位 = 5");
System.out.println("正数:Math.round(11.5) = " + Math.round(11.5));
System.out.println("负数:Math.round(-11.5) = " + Math.round(-11.5));
System.out.println("Case2:小数点后第一位 < 5");
System.out.println("正数:Math.round(11.49) = " + Math.round(11.49));
System.out.println("负数:Math.round(-11.49) = " + Math.round(-11.49));
System.out.println("Case3:小数点后第一位 > 5");
System.out.println("正数:Math.round(11.69) = " + Math.round(11.69));
System.out.println("负数:Math.round(-11.69) = " + Math.round(-11.69));
System.out.println("结论:正数小数点后大于5则进位;负数小数点后小于以及等于5都舍去,大于5的则进位");
System.out.println("也就是说:小数点后大于5全部加,等于5正数加,小于5全不加");
}
}
Parameters
| d | the value to be rounded. |
|---|
Returns
- the closest integer to the argument.
Java Math.round()函数小结的更多相关文章
- Java中Math.round()函数
Math.round(11.5) = 12; Math.round(-11.5) = -11; Math.round()函数是求某个数的整数部分,且四舍五入.
- Math.Round函数详解
有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...
- Math.Round函数四舍五入
Math.Round函数四舍五入的问题 今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...
- 一场Math.Round函数的误解
有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...
- 【JAVA】Math.Round()函数常见问题“四舍5入”
java.lang.Math.Round()使用时候,处理方式整理,方便以后查找 /** * 测试函数 2014-01-10 */ public class TestMath { pu ...
- Math.round() 函数返回一个数字四舍五入后最接近的整数。
语法: Math.round(x); 参数:x 返回值:给定数字的值四舍五入到最接近的整数 描述: 如果参数的小数部分大于 0.5,则舍入到相邻的绝对值更大的整数. 如果参数的小数部分小于 0.5,则 ...
- Math.Round四舍五入
Math.Round函数四舍五入的问题 今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...
- .Net中Math.Round与四舍五入
有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...
- Math.floor,Math.ceil,Math.rint,Math.round用法
一.Math.floor函数讲解 floor原意:地板.Math.floor函数是求一个浮点数的地板,就是求一个最接近它的整数,它的值小于或等于这个浮点数.看下面的例子: package com.qi ...
随机推荐
- Python开发个人专属表情包网站
“表情包”是一种利用图片来表示感情的一种方式.表情包是在社交软件活跃之后,形成的一种流行文化,表情包流行于互联网上面,基本人人都会发表情. 曾经你是否也有过找不到表情包去应对别人的时候. 今天小编分享 ...
- Matlab利用subplot绘制多个图像
利用subplot绘制多个图像 subplot(m,n,p) subplot是将多个图画到一个平面上的函数,m是行,n是列,p是所要绘制图所在的位置 x = 0:0.1:100; sinY = sin ...
- Win10中小娜无法搜索本地应用
解决方案 1.win+X - Windows PowerShell(管理员) 2. 输入Get-AppXPackage -Name Microsoft.Windows.Cortana | Fo ...
- 2019年5月17日A股暴跌行情思考
2019年5月17日A股暴跌行情思考 原因:特朗普针对华为的禁商令,人民币对美元汇率大跌 盘面:平开,单边下跌,收盘80多股跌停 操作:下午2点加仓,满仓 总结: 本次操作太过激进. 局势不明朗时抄底 ...
- CSP2019 括号树
Description: 给定括号树,每个节点都是 ( 或 ) ,定义节点的权值为根到该节点的简单路径所构成的括号序列中不同合法子串的个数(子串需要连续,子串所在的位置不同即为不同.)与节点编号的乘积 ...
- 一步一步配置docker(tomcat+jenkins+phpmyadmin+nginx)
经过半个月的docker学习实践,今天对自己的学习成果做个总结. 貌似官方推荐的是docker compose使用DockerFile 来配置,但目前还没学习使用docker compose,先学习通 ...
- Java入门程序“hello,world!”
1.程序开发步骤说明 开发环境已经搭建完毕,可以开发我们第一个Java程序了. Java程序开发三步骤:编写.编译.运行.(图片介绍) 2.编写Java程序 新建一个普通的记事本,给其命名为Hel ...
- 源码安装openldap(转)
Ubuntu安装OpenLDAP(附错误的详细解决办法) 1 下载OpenLDAP源码 http://www.openldap.org/software/download/ 或者 ftp://ftp. ...
- idea2019 3.3最新版本破解安装教程
直接给上神秘地址得了:(应该都可以破解) https://www.jianshu.com/p/c7bdc5819d31
- (十四)登陆注册 逻辑二 前端globalData的使用 和 Storage
我们在点击登录的时候 成功之后跳转到home 一 在全局的app.js里会有一个 globalData 因为还有其他页面也要使用 共有的数据 所有用到 gl ...