Rounding Negative Numbers in Java

According to Wikipedia, when rounding a negative number, you bypass the absolute number. Thus, by this reasoning, -3.5 will be rounded to -4. But when I use java.lang.Math.round (-3.5), -3 is returned. Can someone explain this?

+18
java rounding
Nov 06 '08 at 18:18
source share
6 answers

According to javadoc

Returns the closest long argument. The result is rounded to the nearest integer by adding 1/2, taking the floor of the result and casting the result to print for a long time. In other words, the result is equal to the value of expression:

(long)Math.floor(a + 0.5d)

Conceptually, you round up . In other words, the next integer is greater than the value and -3 is greater than -3.5, and -4 is less.

+16
Nov 06 '08 at 18:21
source share

There are many rounding methods; the one you are looking at is called symmetric arithmetic rounding (as he claims). The section you refer to reads: "This method is commonly used in mathematical applications, such as accounting, which is commonly used in elementary mathematics classes." This, apparently, confirms that this is not a rule that is agreed on a global level, namely the one that is most common.

Personally, I don’t remember ever teaching this rule at school. My understanding of rounding has always been that .5 is rounded, regardless of the sign of the number. Java authors seem to have the same understanding. This is an asymmetric arithmetic rounding.

Different tools and languages ​​can potentially use different rounding patterns. Excel apparently uses the symmetric method.

(In general, I would advise that if you find a conflict between Wikipedia and experience, you are looking for information elsewhere. Wikipedia is not perfect.)

+8
Nov 06 '08 at 18:23
source share

For what it's worth, java.math.BigDecimal has custom rounding modes if you need more control over such things.

+5
Nov 06 '08 at 19:35
source share

The Wikipedia article you are quoting does not say that the only way to round is just a general way of rounding. Several alternatives are also mentioned in this article (unfortunately, none of them describe the Java rounding method, although they call it "Asymmetric Arithmetic Rounding" when indicating what JavaScript does).

You need to decide how you want to round the numbers, and then use this method. If the Java implementation matches this, then fine. otherwise you will need to implement it yourself.

+4
Nov 06 '08 at 18:31
source share

According to Javadocs:

Returns the closest long to an argument. The result is rounded to an integer, adding 1/2, taking the result word and returning the result as long . In other words, the result is equal to the value of the expression:

  (long) Math.floor (a + 0.5d) 
+2
Nov 06 '08 at 18:24
source share

Turns off convention for rounding. I think Wikipedia is wrong. It turns out that Microsoft is mistaken, although there’s also -4 around it, which is not conditional (I checked with someone who has a candidate for mathematics).

0
Nov 06 '08 at 18:29
source share



All Articles