6 Replies Latest reply: Oct 22, 2008 5:20 PM by 807589 RSS

    BigDecimal Round Half Up  - Not Working Properly

    807589
      BigDecimal's ROUND_HALF_UP method is producing some strange results for me. I'm trying to round off a double to decimal places.

      Here is the description of the method from the API

      "Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for ROUND_UP if the discarded fraction is >= .5; otherwise, behaves as for ROUND_DOWN. Note that this is the rounding mode that most of us were taught in grade school."

      However, I've found that if the discarded fraction =5 it rounds down, not up.

      e.g.
      bd = new BigDecimal(12.825);
      bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
      double result = bd.doubleValue();
      result=12.82!!!!

      I noticed on IBM's list of JAVA fixes, it mentions a fix to BigDecimal's Round_Half_Up method but for older versions of Java (1.4). I'm running the newest version, but surely this is the same problem they're talking about.
           
      Please help!!!