ICU-8761 Merging #6408 DecimalFormat fraction digit problem with rounding + BigDecimal (r29797) from trunk.

X-SVN-Rev: 30524
This commit is contained in:
Yoshito Umaoka 2011-08-16 21:53:03 +00:00
parent 235e21e5da
commit 62879301b3
2 changed files with 31 additions and 8 deletions

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 1996-2010, International Business Machines Corporation and *
* Copyright (C) 1996-2011, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
@ -533,7 +533,7 @@ final class DigitList {
* Upon return, count will be less than or equal to maximumDigits.
* This now performs rounding when maximumDigits is 0, formerly it did not.
*/
public final void round(int maximumDigits) {
public final void round(int maximumDigits) {
// Eliminate digits beyond maximum digits to be displayed.
// Round up if appropriate.
// [bnf] rewritten to fix 4179818
@ -562,12 +562,12 @@ final class DigitList {
++maximumDigits; // Increment for use as count
}
count = maximumDigits;
/*Bug 4217661 DecimalFormat formats 1.001 to "1.00" instead of "1"
Eliminate trailing zeros. [Richard/GCL]
*/
while (count > 1 && digits[count-1] == '0') {
--count;
} //[Richard/GCL]
}
// Bug 4217661 DecimalFormat formats 1.001 to "1.00" instead of "1"
// Eliminate trailing zeros. [Richard/GCL]
// [dlf] moved outside if block, see ticket #6408
while (count > 1 && digits[count-1] == '0') {
--count;
}
}

View file

@ -1642,6 +1642,29 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
}
}
public void TestBigDecimalRounding() {
String figure = "50.000000004";
Double dbl = new Double(figure);
BigDecimal dec = new BigDecimal(figure);
DecimalFormat f = (DecimalFormat) NumberFormat.getInstance();
f.applyPattern("00.00######");
assertEquals("double format", "50.00", f.format(dbl));
assertEquals("bigdec format", "50.00", f.format(dec));
int maxFracDigits = f.getMaximumFractionDigits();
BigDecimal roundingIncrement = new BigDecimal("1").movePointLeft(maxFracDigits);
f.setRoundingIncrement(roundingIncrement);
f.setRoundingMode(BigDecimal.ROUND_DOWN);
assertEquals("Rounding down", f.format(dbl), f.format(dec));
f.setRoundingIncrement(roundingIncrement);
f.setRoundingMode(BigDecimal.ROUND_HALF_UP);
assertEquals("Rounding half up", f.format(dbl), f.format(dec));
}
void checkRounding(DecimalFormat nf, BigDecimal base, int iterations, BigDecimal increment) {
nf.setRoundingIncrement(increment.toBigDecimal());
BigDecimal lastParsed = new BigDecimal(Integer.MIN_VALUE); // used to make sure that rounding is monotonic