src/java.base/share/classes/java/text/DigitList.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -518,23 +518,43 @@
                         return isNegative;
                     }
                 }
                 break;
             case HALF_UP:
-                if (digits[maximumDigits] >= '5') {
-                    // We should not round up if the rounding digits position is
-                    // exactly the last index and if digits were already rounded.
-                    if ((maximumDigits == (count - 1)) &&
-                        (alreadyRounded))
+                if (digits[maximumDigits] > '5') {
+                    // Value was above tie, whatever did FloatingDecimal
+                    return true;
+                }
+                else if (digits[maximumDigits] == '5') {
+                    // Digit at rounding position is a '5'. Tie cases.
+                    if (maximumDigits != (count - 1))
+                        // Remaining digits. Above tie  => must round-up
+                        return true;
+                    else {
+                        // Digit at rounding position is the last one !
+                        if (allDecimalDigits)
+                            // Exact binary representation. On the tie.
+                            // Strictly follow HALF_UP rule ==> round-up
+                            return true;
+                        else {
+                            if (alreadyRounded)
+                                // Digit sequence rounded-up. Was below tie.
+                                // Don't round-up twice !
                         return false;
-
-                    // Value was exactly at or was above tie. We must round up.
+                            else
+                                // Digit sequence truncated. Was above tie
+                                // HALF_UP rule ==>  must round-up !
                     return true;
                 }
+                    }
+                }
+                // Digit at rounding position is < '5' ==> no round-up.
+                // Just let do the default, which is no round-up (thus break).
                 break;
             case HALF_DOWN:
                 if (digits[maximumDigits] > '5') {
+                    // Value was above tie, whatever did FloatingDecimal
                     return true;
                 } else if (digits[maximumDigits] == '5' ) {
                     if (maximumDigits == (count - 1)) {
                         // The rounding position is exactly the last index.
                         if (allDecimalDigits || alreadyRounded)

@@ -543,11 +563,11 @@
                              * an exact tie). We should not round up, following
                              * the HALF_DOWN rounding rule.
                              */
                             return false;
                         else
-                            // Value was above the tie, we must round up.
+                            // Value was above tie, we must round up.
                             return true;
                     }
 
                     // We must round up if it gives a non null digit after '5'.
                     for (int i=maximumDigits+1; i<count; ++i) {