changeset 7654:2ee772cda1d6

6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0") Summary: Make stripTrailingZeros() return BigDecimal.ZERO if the BigDecimal is numerically equal to zero. Reviewed-by: darcy Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>
author bpb
date Tue, 09 Jul 2013 12:47:37 -0700
parents 607fa1ff3de2
children 69d9fe8175a0
files src/share/classes/java/math/BigDecimal.java test/java/math/BigDecimal/StrippingZerosTest.java
diffstat 2 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/math/BigDecimal.java	Tue Jul 09 11:26:25 2013 -0700
+++ b/src/share/classes/java/math/BigDecimal.java	Tue Jul 09 12:47:37 2013 -0700
@@ -2592,14 +2592,18 @@
      * the {@code BigDecimal} value {@code 600.0}, which has
      * [{@code BigInteger}, {@code scale}] components equals to
      * [6000, 1], yields {@code 6E2} with [{@code BigInteger},
-     * {@code scale}] components equals to [6, -2]
+     * {@code scale}] components equals to [6, -2].  If
+     * this BigDecimal is numerically equal to zero, then
+     * {@code BigDecimal.ZERO} is returned.
      *
      * @return a numerically equal {@code BigDecimal} with any
      * trailing zeros removed.
      * @since 1.5
      */
     public BigDecimal stripTrailingZeros() {
-        if(intCompact!=INFLATED) {
+        if (intCompact == 0 || (intVal != null && intVal.signum() == 0)) {
+            return BigDecimal.ZERO;
+        } else if (intCompact != INFLATED) {
             return createAndStripZerosToMatchScale(intCompact, scale, Long.MIN_VALUE);
         } else {
             return createAndStripZerosToMatchScale(intVal, scale, Long.MIN_VALUE);
--- a/test/java/math/BigDecimal/StrippingZerosTest.java	Tue Jul 09 11:26:25 2013 -0700
+++ b/test/java/math/BigDecimal/StrippingZerosTest.java	Tue Jul 09 12:47:37 2013 -0700
@@ -45,8 +45,17 @@
             {new BigDecimal("1234.56780"),      new BigDecimal("1234.5678")},
             {new BigDecimal("1234.567800000"),  new BigDecimal("1234.5678")},
             {new BigDecimal("0"),               new BigDecimal("0")},
-            {new BigDecimal("0e100"),           new BigDecimal("0e100")},
-            {new BigDecimal("0e-100"),          new BigDecimal("0e-100")},
+            {new BigDecimal("0e2"),             BigDecimal.ZERO},
+            {new BigDecimal("0e-2"),            BigDecimal.ZERO},
+            {new BigDecimal("0e42"),            BigDecimal.ZERO},
+            {new BigDecimal("+0e42"),           BigDecimal.ZERO},
+            {new BigDecimal("-0e42"),           BigDecimal.ZERO},
+            {new BigDecimal("0e-42"),           BigDecimal.ZERO},
+            {new BigDecimal("+0e-42"),          BigDecimal.ZERO},
+            {new BigDecimal("-0e-42"),          BigDecimal.ZERO},
+            {new BigDecimal("0e-2"),            BigDecimal.ZERO},
+            {new BigDecimal("0e100"),           BigDecimal.ZERO},
+            {new BigDecimal("0e-100"),          BigDecimal.ZERO},
             {new BigDecimal("10"),              new BigDecimal("1e1")},
             {new BigDecimal("20"),              new BigDecimal("2e1")},
             {new BigDecimal("100"),             new BigDecimal("1e2")},