diff --git a/src/share/classes/java/util/Formatter.java b/src/share/classes/java/util/Formatter.java index ad4dca1d3e66e41da4d3a496feafcf70cc63dc16..5100adbd27c2daecb6ec10100139008a4eb1f2c7 100644 --- a/src/share/classes/java/util/Formatter.java +++ b/src/share/classes/java/util/Formatter.java @@ -39,6 +39,7 @@ import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; +import java.math.RoundingMode; import java.nio.charset.Charset; import java.text.DateFormatSymbols; import java.text.DecimalFormat; @@ -1252,7 +1253,7 @@ import sun.misc.FormattedFloatingDecimal; * Double#toString(double)} respectively, then the value will be rounded * using the {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up * algorithm}. Otherwise, zeros may be appended to reach the precision. - * For a canonical representation of the value,use {@link + * For a canonical representation of the value, use {@link * Float#toString(float)} or {@link Double#toString(double)} as * appropriate. * @@ -3569,15 +3570,23 @@ public final class Formatter implements Closeable, Flushable { // Create a new BigDecimal with the desired precision. int prec = (precision == -1 ? 6 : precision); int scale = value.scale(); - int compPrec = value.precision(); - if (scale > prec) - compPrec -= (scale - prec); - MathContext mc = new MathContext(compPrec); - BigDecimal v - = new BigDecimal(value.unscaledValue(), scale, mc); - BigDecimalLayout bdl - = new BigDecimalLayout(v.unscaledValue(), v.scale(), + if (scale > prec) { + // more "scale" digits than the requested "precision + int compPrec = value.precision(); + if (compPrec <= scale) { + // case of 0.xxxxxx + value = value.setScale(prec, RoundingMode.HALF_UP); + } else { + compPrec -= (scale - prec); + value = new BigDecimal(value.unscaledValue(), + scale, + new MathContext(compPrec)); + } + } + BigDecimalLayout bdl = new BigDecimalLayout( + value.unscaledValue(), + value.scale(), BigDecimalLayoutForm.DECIMAL_FLOAT); char mant[] = bdl.mantissa(); diff --git a/test/java/util/Formatter/Basic-X.java b/test/java/util/Formatter/Basic-X.java index b4a603f7bcd1992ee73dcc3aa3b290111c480169..4677062cc14ed83c553d05f1d9bf975b0378608f 100644 --- a/test/java/util/Formatter/Basic-X.java +++ b/test/java/util/Formatter/Basic-X.java @@ -1054,6 +1054,52 @@ public class Basic$Type$ extends Basic { test("%4.1f", " 1.0", val); test("%4.2f", "0.99", val); test("%4.3f", "0.990", val); + + // #6476425 + val = new BigDecimal("0.00001"); + test("%.0f", "0", val); + test("%.1f", "0.0", val); + test("%.2f", "0.00", val); + test("%.3f", "0.000", val); + test("%.4f", "0.0000", val); + test("%.5f", "0.00001", val); + + val = new BigDecimal("1.00001"); + test("%.0f", "1", val); + test("%.1f", "1.0", val); + test("%.2f", "1.00", val); + test("%.3f", "1.000", val); + test("%.4f", "1.0000", val); + test("%.5f", "1.00001", val); + + val = new BigDecimal("1.23456"); + test("%.0f", "1", val); + test("%.1f", "1.2", val); + test("%.2f", "1.23", val); + test("%.3f", "1.235", val); + test("%.4f", "1.2346", val); + test("%.5f", "1.23456", val); + test("%.6f", "1.234560", val); + + val = new BigDecimal("9.99999"); + test("%.0f", "10", val); + test("%.1f", "10.0", val); + test("%.2f", "10.00", val); + test("%.3f", "10.000", val); + test("%.4f", "10.0000", val); + test("%.5f", "9.99999", val); + test("%.6f", "9.999990", val); + + + val = new BigDecimal("1.99999"); + test("%.0f", "2", val); + test("%.1f", "2.0", val); + test("%.2f", "2.00", val); + test("%.3f", "2.000", val); + test("%.4f", "2.0000", val); + test("%.5f", "1.99999", val); + test("%.6f", "1.999990", val); + #end[BigDecimal] #if[float] diff --git a/test/java/util/Formatter/Basic.java b/test/java/util/Formatter/Basic.java index 22d92495d49a57752f238e01ffade11b253dd6fc..3a957b2f4faded9e112a94aa756b7d8531a9256c 100644 --- a/test/java/util/Formatter/Basic.java +++ b/test/java/util/Formatter/Basic.java @@ -25,7 +25,7 @@ * @summary Unit test for formatter * @bug 4906370 4962433 4973103 4989961 5005818 5031150 4970931 4989491 5002937 * 5005104 5007745 5061412 5055180 5066788 5088703 6317248 6318369 6320122 - * 6344623 6369500 6534606 6282094 6286592 + * 6344623 6369500 6534606 6282094 6286592 6476425 * * @run shell/timeout=240 Basic.sh */ diff --git a/test/java/util/Formatter/BasicBigDecimal.java b/test/java/util/Formatter/BasicBigDecimal.java index d35da2565769c730426e9de4acfd38a24bf3cb00..da96391384c29325a3111e7eb288a0d23650f5a1 100644 --- a/test/java/util/Formatter/BasicBigDecimal.java +++ b/test/java/util/Formatter/BasicBigDecimal.java @@ -1055,6 +1055,52 @@ public class BasicBigDecimal extends Basic { test("%4.2f", "0.99", val); test("%4.3f", "0.990", val); + // #6476425 + val = new BigDecimal("0.00001"); + test("%.0f", "0", val); + test("%.1f", "0.0", val); + test("%.2f", "0.00", val); + test("%.3f", "0.000", val); + test("%.4f", "0.0000", val); + test("%.5f", "0.00001", val); + + val = new BigDecimal("1.00001"); + test("%.0f", "1", val); + test("%.1f", "1.0", val); + test("%.2f", "1.00", val); + test("%.3f", "1.000", val); + test("%.4f", "1.0000", val); + test("%.5f", "1.00001", val); + + val = new BigDecimal("1.23456"); + test("%.0f", "1", val); + test("%.1f", "1.2", val); + test("%.2f", "1.23", val); + test("%.3f", "1.235", val); + test("%.4f", "1.2346", val); + test("%.5f", "1.23456", val); + test("%.6f", "1.234560", val); + + val = new BigDecimal("9.99999"); + test("%.0f", "10", val); + test("%.1f", "10.0", val); + test("%.2f", "10.00", val); + test("%.3f", "10.000", val); + test("%.4f", "10.0000", val); + test("%.5f", "9.99999", val); + test("%.6f", "9.999990", val); + + + val = new BigDecimal("1.99999"); + test("%.0f", "2", val); + test("%.1f", "2.0", val); + test("%.2f", "2.00", val); + test("%.3f", "2.000", val); + test("%.4f", "2.0000", val); + test("%.5f", "1.99999", val); + test("%.6f", "1.999990", val); + + diff --git a/test/java/util/Formatter/BasicBigInteger.java b/test/java/util/Formatter/BasicBigInteger.java index e93f60d398d7666c0b0e37d3cb72ef66dbb87ad8..4bba5a0fbe4889661f7ecc695fe37ef80c587feb 100644 --- a/test/java/util/Formatter/BasicBigInteger.java +++ b/test/java/util/Formatter/BasicBigInteger.java @@ -1503,6 +1503,52 @@ public class BasicBigInteger extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicBoolean.java b/test/java/util/Formatter/BasicBoolean.java index c7af21083aa6ac49fc00215bb6aeb43f4eb4509a..ea60e388652e9d62c539a863df5fb2d252ca0d8f 100644 --- a/test/java/util/Formatter/BasicBoolean.java +++ b/test/java/util/Formatter/BasicBoolean.java @@ -1503,6 +1503,52 @@ public class BasicBoolean extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicBooleanObject.java b/test/java/util/Formatter/BasicBooleanObject.java index 81c1f3e1ada51e5ca06c298ff0fb1315b2c15093..b4fbabb76b80b883836e09d8df99f4edc74265a6 100644 --- a/test/java/util/Formatter/BasicBooleanObject.java +++ b/test/java/util/Formatter/BasicBooleanObject.java @@ -1503,6 +1503,52 @@ public class BasicBooleanObject extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicByte.java b/test/java/util/Formatter/BasicByte.java index ff44286fdc34a3a1adb14021ee3ad3d806372df8..deaf37957e5940056c238c208445747c59e73ad8 100644 --- a/test/java/util/Formatter/BasicByte.java +++ b/test/java/util/Formatter/BasicByte.java @@ -1503,6 +1503,52 @@ public class BasicByte extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicByteObject.java b/test/java/util/Formatter/BasicByteObject.java index e2b158c3e85298da96c5164d92d59aeb304bd23e..06eb68ebd273ab7686b82d852421e8990782a59c 100644 --- a/test/java/util/Formatter/BasicByteObject.java +++ b/test/java/util/Formatter/BasicByteObject.java @@ -1503,6 +1503,52 @@ public class BasicByteObject extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicChar.java b/test/java/util/Formatter/BasicChar.java index bb1b5037e8840f864f2092d601c2ac661b9cf7fb..5ada7b166e70aee2f1141f13846b794abf65b21d 100644 --- a/test/java/util/Formatter/BasicChar.java +++ b/test/java/util/Formatter/BasicChar.java @@ -1503,6 +1503,52 @@ public class BasicChar extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicCharObject.java b/test/java/util/Formatter/BasicCharObject.java index 2d15fa1c52894ee88630f7e67deb8bc27a90ec7e..1e7d05d543f41a747ee3b21f6e8eb7018c520225 100644 --- a/test/java/util/Formatter/BasicCharObject.java +++ b/test/java/util/Formatter/BasicCharObject.java @@ -1503,6 +1503,52 @@ public class BasicCharObject extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicDateTime.java b/test/java/util/Formatter/BasicDateTime.java index 97ae65026bb07028e77fd851efe68854cd1e7922..fd42da06f70b6ee458a3c43b34375c5c18bdff4b 100644 --- a/test/java/util/Formatter/BasicDateTime.java +++ b/test/java/util/Formatter/BasicDateTime.java @@ -1503,6 +1503,52 @@ public class BasicDateTime extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicDouble.java b/test/java/util/Formatter/BasicDouble.java index abbd093fc0a1101f59b523d5a90e5232de76f8bc..d985b46b1a5015806dfec49d5fa100e35ed46fc2 100644 --- a/test/java/util/Formatter/BasicDouble.java +++ b/test/java/util/Formatter/BasicDouble.java @@ -1053,6 +1053,52 @@ public class BasicDouble extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicDoubleObject.java b/test/java/util/Formatter/BasicDoubleObject.java index d49f9e60910646701a7ec1dfcb601038624a91e3..70dfd2929e56b162a9532113f608f1edc8d4b5e4 100644 --- a/test/java/util/Formatter/BasicDoubleObject.java +++ b/test/java/util/Formatter/BasicDoubleObject.java @@ -1053,6 +1053,52 @@ public class BasicDoubleObject extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicFloat.java b/test/java/util/Formatter/BasicFloat.java index 61493b59b989a26a9d9c2dfb43f6af34f11984e3..122b44f1e75708fbdec8b68f78894d57a43e4782 100644 --- a/test/java/util/Formatter/BasicFloat.java +++ b/test/java/util/Formatter/BasicFloat.java @@ -1056,6 +1056,52 @@ public class BasicFloat extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + //--------------------------------------------------------------------- // %f - float diff --git a/test/java/util/Formatter/BasicFloatObject.java b/test/java/util/Formatter/BasicFloatObject.java index fb7bffa150fec3b2a6a85479a7b0272f8a977371..64c874cf2d5354a28d64933ab44b240986862024 100644 --- a/test/java/util/Formatter/BasicFloatObject.java +++ b/test/java/util/Formatter/BasicFloatObject.java @@ -1069,6 +1069,52 @@ public class BasicFloatObject extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicInt.java b/test/java/util/Formatter/BasicInt.java index 1945d5b612ae460961eb1129bc67907a11d26b88..4010d2250e034cf60b995533851faaabb068a91c 100644 --- a/test/java/util/Formatter/BasicInt.java +++ b/test/java/util/Formatter/BasicInt.java @@ -1503,6 +1503,52 @@ public class BasicInt extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicIntObject.java b/test/java/util/Formatter/BasicIntObject.java index 267ffd2eb952fb39093bd41b3c4214dade359478..fe41ea2964b98dc96d3d14d0831ad77b0fcace82 100644 --- a/test/java/util/Formatter/BasicIntObject.java +++ b/test/java/util/Formatter/BasicIntObject.java @@ -1503,6 +1503,52 @@ public class BasicIntObject extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicLong.java b/test/java/util/Formatter/BasicLong.java index 151dcb7897544d4163b365c28a2e1d43d0f94a9f..fe232f1978e0d732603f80d216469b8a25d69652 100644 --- a/test/java/util/Formatter/BasicLong.java +++ b/test/java/util/Formatter/BasicLong.java @@ -1503,6 +1503,52 @@ public class BasicLong extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicLongObject.java b/test/java/util/Formatter/BasicLongObject.java index 3944418b3a65f7d352e9ba2d693d30fb2eb36e16..c99e0ba3c2acb5494e641c7ddf6655fb063f2024 100644 --- a/test/java/util/Formatter/BasicLongObject.java +++ b/test/java/util/Formatter/BasicLongObject.java @@ -1503,6 +1503,52 @@ public class BasicLongObject extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicShort.java b/test/java/util/Formatter/BasicShort.java index 5554c15382d873f717d17961d7c4f07b20dc8eeb..39079e4ad1e1c9970726e19fb966c6e2990812aa 100644 --- a/test/java/util/Formatter/BasicShort.java +++ b/test/java/util/Formatter/BasicShort.java @@ -1503,6 +1503,52 @@ public class BasicShort extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/BasicShortObject.java b/test/java/util/Formatter/BasicShortObject.java index 629fb573674eebc240df5db917136d435ef01819..e2dcc2301740e6866352d81aec81297e88f1722a 100644 --- a/test/java/util/Formatter/BasicShortObject.java +++ b/test/java/util/Formatter/BasicShortObject.java @@ -1503,6 +1503,52 @@ public class BasicShortObject extends Basic { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/java/util/Formatter/genBasic.sh b/test/java/util/Formatter/genBasic.sh index 493e0360444d11072b0897767d1516a2e9fdccda..2c352ad39cf0b817718bd06f76a0963ef18206b3 100644 --- a/test/java/util/Formatter/genBasic.sh +++ b/test/java/util/Formatter/genBasic.sh @@ -23,14 +23,14 @@ # have any questions. # -SPP='sh ../../../../make/java/nio/spp.sh' +javac -d . ../../../../make/tools/src/build/tools/spp/Spp.java gen() { # if [ $3 = "true" ] # then $SPP -K$1 -Dtype=$1 -DType=$2 -KprimBasic$2.java # else $SPP -K$1 -Dtype=$1 -DType=$2 -K$3 Basic$2.java # fi - $SPP -K$1 -Dtype=$1 -DType=$2 -K$3 -K$4 -K$5 -K$6 Basic$2.java + java build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 -K$3 -K$4 -K$5 -K$6 Basic$2.java } gen boolean Boolean prim "" "" "" @@ -54,3 +54,5 @@ gen Double DoubleObject "" fp "" "" gen BigDecimal BigDecimal "" fp "" "" gen Calendar DateTime "" "" "" datetime + +rm -rf build