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

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 1996, 2013, 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 --- 1,7 ---- /* ! * 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
*** 307,316 **** --- 307,317 ---- * an exact decimal representation of the double that was passed. */ private void set(boolean isNegative, String s, boolean roundedUp, boolean allDecimalDigits, int maximumDigits, boolean fixedPoint) { + this.isNegative = isNegative; int len = s.length(); char[] source = getDataChars(len); s.getChars(0, len, source, 0);
*** 380,389 **** --- 381,391 ---- // Eliminate digits beyond maximum digits to be displayed. // Round up if appropriate. round(fixedPoint ? (maximumDigits + decimalAt) : maximumDigits, roundedUp, allDecimalDigits); + } /** * Round the representation to the given number of digits. * @param maximumDigits The maximum number of digits to be shown.
*** 438,447 **** --- 440,452 ---- * [bnf] * @param maximumDigits the number of digits to keep, from 0 to * <code>count-1</code>. If 0, then all digits are rounded away, and * this method returns true if a one should be generated (e.g., formatting * 0.09 with "#.#"). + * @param alreadyRounded Boolean indicating if rounding up already happened. + * @param allDecimalDigits Boolean indicating if the digits provide an exact + * representation of the value. * @exception ArithmeticException if rounding is needed with rounding * mode being set to RoundingMode.UNNECESSARY * @return true if digit <code>maximumDigits-1</code> should be * incremented */
*** 518,563 **** 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)) - return false; - - // Value was exactly at or was above tie. We must round up. - return true; - } - break; case HALF_DOWN: if (digits[maximumDigits] > '5') { return true; ! } else if (digits[maximumDigits] == '5' ) { ! if (maximumDigits == (count - 1)) { ! // The rounding position is exactly the last index. ! if (allDecimalDigits || alreadyRounded) ! /* FloatingDecimal rounded up (value was below tie), ! * or provided the exact list of digits (value was ! * 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. return true; } ! ! // We must round up if it gives a non null digit after '5'. ! for (int i=maximumDigits+1; i<count; ++i) { ! if (digits[i] != '0') { return true; } } } break; case HALF_EVEN: // Implement IEEE half-even rounding if (digits[maximumDigits] > '5') { return true; --- 523,573 ---- return isNegative; } } break; case HALF_UP: case HALF_DOWN: if (digits[maximumDigits] > '5') { + // Value is above tie ==> must round-up return true; ! } ! else if (digits[maximumDigits] == '5') { ! // Digit at rounding position is a '5'. Tie cases. ! if (maximumDigits != (count - 1)) { ! // There are 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. ! if (roundingMode == RoundingMode.HALF_UP) { ! // Strictly follow HALF_UP rule ==> round-up return true; } + else { + // Strictly follow HALF_DOWN rule ==> don't round-up + return false; + } + } + else { + // Not an exact binary representation. + if (alreadyRounded) { + // Digit sequence rounded-up. Was below tie. + // Don't round-up twice ! + return false; + } + else { + // Digit sequence truncated. Was above tie. + // 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_EVEN: // Implement IEEE half-even rounding if (digits[maximumDigits] > '5') { return true;