在 Java 中舍入负数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/269721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding negative numbers in Java
提问by Elie
According to Wikipediawhen rounding a negative number, you round the absolute number. So by that reasoning, -3.5 would be rounded to -4. But when I use java.lang.Math.round(-3.5) returns -3. Can someone please explain this?
根据维基百科,在舍入负数时,您舍入绝对数。因此,根据这种推理,-3.5 将四舍五入为 -4。但是当我使用 java.lang.Math.round(-3.5) 时返回 -3。有人可以解释一下吗?
采纳答案by sblundy
According to the javadoc
根据javadoc
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)
返回最接近参数的 long。通过加 1/2、取结果的下限并将结果转换为 long 类型,结果四舍五入为整数。换句话说,结果等于表达式的值:
(long)Math.floor(a + 0.5d)
Conceptually, you round up. In other words, to the next integer greaterthan the value and -3 is greater than -3.5, while -4 is less.
从概念上讲,您将. 换句话说,对下一个大于该值的整数,-3 大于-3.5,而-4 小于。
回答by Dave Costa
There are a variety of methods of rounding; the one you are looking at is called Symmetrical Arithmetic Rounding (as it states). The section you are referring to states: "This method occurs commonly used in mathematical applications, for example in accounting. It is the one generally taught in elementary mathematics classes." This seems to acknowledge that it is not a rule that is globally agreed upon, just the one that is most common.
有多种舍入方法;您正在查看的称为对称算术舍入(如其所述)。您所指的部分指出:“这种方法通常用于数学应用程序,例如会计。它是小学数学课程中通常教授的方法。” 这似乎承认它不是全球公认的规则,只是最常见的规则。
Personally, I don't recall ever being taught that rule in school. My understanding of rounding has always been that .5 is rounded up, regardless of the sign of the number. Apparently the authors of Java have the same understanding. This is Asymmetrical Arithmetic Rounding.
就我个人而言,我不记得在学校里教过这条规则。我对四舍五入的理解一直是四舍五入,无论数字的符号如何。显然Java的作者也有同样的理解。这是不对称算术舍入。
Different tools and languages potentially use different rounding schemes. Excel apparently uses the symmetric method.
不同的工具和语言可能使用不同的舍入方案。Excel 显然使用对称方法。
(Overall, I would advise that if you find a conflict between Wikipedia and experience, you look for information elsewhere. Wikipedia is not perfect.)
(总的来说,我建议如果你发现维基百科和经验之间存在冲突,你可以在别处寻找信息。维基百科并不完美。)
回答by Dave Costa
For what it's worth, java.math.BigDecimalhas selectable rounding modes if you need more control over that sort of thing.
就其价值而言,java.math.BigDecimal如果您需要对此类事情进行更多控制,则可以选择舍入模式。
回答by Michael Burr
The Wikipedia article you cite does not say that's the only way to round, just the common way to round. Also mentioned in that article are several alternatives (unfortunately none of which describe Java's rounding method - even though they call it out as "Asymmetric Arithmetic Rounding" when indicating what JavaScript does).
您引用的维基百科文章并没有说这是唯一的四舍五入方式,只是常用的四舍五入方式。在那篇文章中还提到了几种替代方法(不幸的是,它们都没有描述 Java 的舍入方法——尽管在指示 JavaScript 的作用时,他们将其称为“非对称算术舍入”)。
You need to decide how you wantyour numbers rounded, then use that method. If Java's implementation matches that, then great. otherwise you'll need to implement it on your own.
你需要决定你如何想你的数字四舍五入,然后使用该方法。如果 Java 的实现与此匹配,那就太好了。否则你需要自己实现它。
回答by Paulo Guedes
According to Javadocs:
根据 Javadocs:
Returns the closest longto 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参数的值。通过加 1/2、取结果的下限并将结果转换为 type ,结果四舍五入为整数long。换句话说,结果等于表达式的值:
(long)Math.floor(a + 0.5d)
回答by Elie
Turns out the convention is to round up. I guess Wikipedia is fallible. Turns out Microsoft got it wrong, though, as they round it to -4 as well, which is not convention (I checked with someone who has a PhD in math).
事实证明,惯例是四舍五入。我猜维基百科是错误的。事实证明,微软弄错了,因为他们也把它四舍五入到 -4,这不是惯例(我与拥有数学博士学位的人核对过)。

