详解Java中Math.round()的取整规则
作者:shruber 时间:2022-10-20 14:22:01
做Java的面试题时遇到了以下这题,百度了一下Math.round()的修约规则,有的说是四舍五入,有的说是四舍六入,发现和我学分析化学时用的数字修约规则(四舍六入五成双)很像,所以验证一下;
原题:Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?
作者给的解题方法如下:
答:Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在参数上加0.5然后进行下取整。
先说结论,题目作者给的解释是对的,后来找了该方法的定义,结果方法的定义就是这个原理,果然看文档才是王道;
round方法:
static long round(double a)
此方法返回的参数最接近的long.
static int round(float a)
此方法返回的参数最接近的整数.
注:四舍六入五成双:
当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,这种修约(舍入)规则是“四舍六入五成双”,也即“4舍6入5凑偶”这里“四”是指≤4 时舍去,”六”是指≥6时进上,”五”指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:①5前为奇数,舍5入1;②5前为偶数,舍5不进。(0是偶数)
以下只论证static int round(float a)
//四舍
int[] test1 = {
Math.round(2.40f),
Math.round(2.44f),
Math.round(2.45f),
Math.round(2.46f),
Math.round(-2.40f),
Math.round(-2.44f),
Math.round(-2.45f),
Math.round(-2.46f),
Math.round(3.40f),
Math.round(3.44f),
Math.round(3.45f),
Math.round(3.46f),
Math.round(-3.40f),
Math.round(-3.44f),
Math.round(-3.45f),
Math.round(-3.46f)};
for(int i = 0; i< test1.length; i++)
{
System.out.print(test1[i]+",");
}
//输出:2,2,2,2,-2,-2,-2,-2,3,3,3,3,-3,-3,-3,-3,符合四舍;也符合 加0.5,进行下取整;
//六入
int[] test2 = {
Math.round(2.60f),
Math.round(2.64f),
Math.round(2.65f),
Math.round(2.66f),
Math.round(-2.60f),
Math.round(-2.64f),
Math.round(-2.65f),
Math.round(-2.66f),
Math.round(3.60f),
Math.round(3.64f),
Math.round(3.65f),
Math.round(3.66f),
Math.round(-3.60f),
Math.round(-3.64f),
Math.round(-3.65f),
Math.round(-3.66f)};
for(int i = 0; i< test2.length; i++)
{
System.out.print(test2[i]+",");
}
//输出:3,3,3,3,-3,-3,-3,-3,4,4,4,4,-4,-4,-4,-4,符合六入;也符合 加0.5,进行下取整;
//五成双之五后无数字
int[] test3 = {
Math.round(2.5f),
Math.round(-2.5f),
Math.round(3.5f),
Math.round(-3.5f)};
for(int i = 0; i< test3.length; i++)
{
System.out.print(test3[i]+",");
}
//输出:3,-2,4,-3,不符合五成双;符合 加0.5,进行下取整;
//五成双之五后有数字(零,非零)
int[] test4 = {
Math.round(2.50f),
Math.round(2.51f),
Math.round(2.59f),
Math.round(-2.50f),
Math.round(-2.51f),
Math.round(-2.59f),
Math.round(3.50f),
Math.round(3.51f),
Math.round(3.59f),
Math.round(-3.50f),
Math.round(-3.51f),
Math.round(-3.59f),
};
for(int i = 0; i< test4.length; i++)
{
System.out.print(test4[i]+",");
}
//输出:3,3,3,-2,-3,-3,4,4,4,-3,-4,-4,不符合五后非零进一;符合 加0.5,进行下取整;
//结论:Math.round()的取整规则不符合四舍六入五成双,以上案例符合 加0.5,进行下取整;
来源:https://blog.csdn.net/shruber/article/details/78413706
标签:Java,Math.round(),取整
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
全面解析java final关键字
2023-03-16 03:04:55
SpringBoot+JSON+AJAX+ECharts+Fiddler实现前后端分离开发可视化
2021-11-12 14:49:17
![](https://img.aspxhome.com/file/2023/2/61562_0s.png)
Java面试必备之ArrayList陷阱解析
2023-02-16 18:14:26
![](https://img.aspxhome.com/file/2023/2/61332_0s.png)
Android通过访问网页查看网页源码实例详解
2023-10-05 03:09:11
C#中Decimal类型截取保留N位小数并且不进行四舍五入操作
2022-01-22 20:55:59
![](https://img.aspxhome.com/file/2023/3/102243_0s.png)
使用JAVA实现邮件发送功能的图文教程
2021-09-04 17:29:36
![](https://img.aspxhome.com/file/2023/9/71949_0s.png)
详解java基于MyBatis使用示例
2023-11-25 09:01:45
![](https://img.aspxhome.com/file/2023/0/59060_0s.png)
Java生成PDF文件的实例代码
2022-05-24 07:22:37
C# 设置防火墙的创建规则
2023-03-21 09:13:42
JAVA多线程之实现用户任务排队并预估排队时长
2022-03-26 03:06:20
![](https://img.aspxhome.com/file/2023/1/80941_0s.gif)
C#中类的使用教程详解
2023-06-12 05:42:42
Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数示例代码
2022-06-06 19:48:18
![](https://img.aspxhome.com/file/2023/4/80224_0s.gif)
Java list如何根据id获取子节点
2023-09-04 09:42:37
Java中volatile关键字实现原理
2023-09-30 14:20:58
![](https://img.aspxhome.com/file/2023/1/63081_0s.png)
Spring Cloud Alibaba Nacos Config配置中心实现
2022-08-02 00:43:05
![](https://img.aspxhome.com/file/2023/0/130910_0s.png)
java Long类型转为json后数据损失精度的处理方式
2022-08-11 12:37:43
![](https://img.aspxhome.com/file/2023/8/61518_0s.png)
Android GestureDetector实现手势滑动效果
2023-10-01 04:37:50
![](https://img.aspxhome.com/file/2023/1/87331_0s.jpg)
SpringBoot使用自动配置xxxAutoConfiguration
2022-11-20 09:05:56
fastjson全局日期序列化设置导致JSONField失效问题解决方案
2021-12-13 10:58:24
C#编程获取IP地址的方法示例
2023-06-16 21:18:20