什么 RoundingMode 常数像 Math.round 一样 100% 工作? [英] What RoundingMode constant works 100% like Math.round?

查看:47
本文介绍了什么 RoundingMode 常数像 Math.round 一样 100% 工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是有没有RoundingMode 常量描述了 Math.round(arg) 的作用?我知道 Math.round 没有正式使用任何 RoundingMode 类/对象.

到目前为止,我没有找到类比.有一个部分类比 - 在 1.5、-1,5 等情况下,关系被打破,就像在 RoundingMode.CEILING 中一样(向正无穷大").但是 RoundingMode.CEILING 类比不能用于其他参数 (1.7, -1.1),因此它不能是 100% 类比.

所以我在所有 RoundingMode 常量中绝对没有发现 100% 的类比,这让我觉得我在某个地方疏忽了.RoundingMode 类中的常量列表应该包含所有广泛使用的舍入模式的常量,对吗?

那么为什么 RoundingMode 类中没有 100% 的类比?

这是比较表用于 RoundingMode 类常量及其行为.

附言Math.floor(arg) 方法的行为与 RoundingMode.FLOOR 完全相同,Math.ceil(arg) 的行为与 RoundingMode.CEILING 完全相同.

解决方案

Math.round()RoundingMode.HALF_UP 对于正数和 RoundingMode 类似.HALF_DOWN 表示负数.

原因是 Math.round() 将关系向正无穷大舍入,而没有模式可以做到这一点.

澄清一下,这两种模式的工作方式与 round() 完全一样,除了平局(即像 xxx.5 这样的数字),例如 5.5 和 -5.5,HALF_DOWN 将舍入为 5 和 -5,HALF_UP 将舍入为 6 和 -6,而 round() 将舍入为 -5 和 6.

您正在寻找的是 RoundingMode.HALF_CEILING 但它不存在.

I mean is there any RoundingMode constant describing exactly what Math.round(arg) does? I understand that Math.round does not formally use any RoundingMode class/object.

So far I found no analogy. There is a partial analogy - in situations like 1.5, -1,5 etc ties are broken just as in RoundingMode.CEILING ("towards positive infinity"). But RoundingMode.CEILING analogy cannot work for other arguments (1.7, -1.1), so it cannot be a 100% analogy.

So I found absolutely no 100% analogy in all RoundingMode constants which makes me think I was inattentive somewhere. List of constants in RoundingMode class shall have constants for all widely-used rounding modes, right?

So why there is no 100% analogy in RoundingMode class?

Here is the comparison table for RoundingMode class constants and their behaviour.

P.S. Math.floor(arg) method behaves exactly like RoundingMode.FLOOR, Math.ceil(arg) behaves exactly like RoundingMode.CEILING.

解决方案

Math.round() works like RoundingMode.HALF_UP for positive numbers and like RoundingMode.HALF_DOWN for negative numbers.

The reason is that Math.round() rounds ties toward positive infinity and there is no mode that does that.

To clarify, both modes will work exactly like round(), except for ties (i.e. numbers like xxx.5), such as 5.5 and -5.5, which will round to 5 and -5 for HALF_DOWN, 6 and -6 for HALF_UP, but -5 and 6 for round().

What you are looking for is RoundingMode.HALF_CEILING but it doesn't exist.

这篇关于什么 RoundingMode 常数像 Math.round 一样 100% 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆