您正在寻找的是 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% 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-15 20:52