9

RoundingMode allows the programmer to specify in what manner floating point numbers are to be rounded. This is great and all, but there is this one thing about it I found peculiar. Maybe I just misunderstood something fundamental at school.

But this rounding mode is described as the one I was taught at school, "Always round to the nearest number, and when dead in the center, always round up.", but why does it round -2.5 to -3?

I conclude as much that it rounds up in terms of absolute values, but -2 is, to me, certainly "up" from -2.5.

Christofer Ohlsson
  • 3,097
  • 4
  • 39
  • 56
  • Have a look at this question: http://stackoverflow.com/questions/269721/rounding-negative-numbers-in-java – Orin MacGregor Dec 21 '12 at 19:09
  • http://docs.oracle.com/javase/1.5.0/docs/api/java/math/RoundingMode.html Take a look at this. This might help you to find the method you are looking for. – Peter Rasmussen Dec 21 '12 at 19:10

2 Answers2

6

RoundingMode.UP is the rounding mode for "away from zero." RoundingMode.FLOOR is towards negative infinity, and CEILING is towards positive infinity. HALF_UP is consistent with UP when the fractional part is exactly 0.5.

They had to choose some term to mean "away from zero."

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
3

The rationale is outlined in the JavaDocs for RoundingMode.HALF_UP.

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for RoundingMode.UP if the discarded fraction is >= 0.5; otherwise, behaves as for RoundingMode.DOWN. Note that this is the rounding mode commonly taught at school.

The Wikipedia article about Rounding methods makes a different claim:

For example, by this rule the value 23.5 gets rounded to 24, but −23.5 gets rounded to −23.

This is one of two rules generally taught in US elementary mathematics classes.

Though a citation has been requested.

Community
  • 1
  • 1
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • 3
    The OP's real issue seems to be the misconception that `RoundingMode.UP` is meant to be interpreted as "towards positive infinity," which is not addressed in this answer currently. – Louis Wasserman Dec 21 '12 at 19:36