src/share/classes/java/math/BigDecimal.java

Print this page
rev 7624 : 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>


2575      * @param n the exponent power of ten to scale by
2576      * @return a BigDecimal whose numerical value is equal to
2577      * ({@code this} * 10<sup>n</sup>)
2578      * @throws ArithmeticException if the scale would be
2579      *         outside the range of a 32-bit integer.
2580      *
2581      * @since 1.5
2582      */
2583     public BigDecimal scaleByPowerOfTen(int n) {
2584         return new BigDecimal(intVal, intCompact,
2585                               checkScale((long)scale - n), precision);
2586     }
2587 
2588     /**
2589      * Returns a {@code BigDecimal} which is numerically equal to
2590      * this one but with any trailing zeros removed from the
2591      * representation.  For example, stripping the trailing zeros from
2592      * the {@code BigDecimal} value {@code 600.0}, which has
2593      * [{@code BigInteger}, {@code scale}] components equals to
2594      * [6000, 1], yields {@code 6E2} with [{@code BigInteger},
2595      * {@code scale}] components equals to [6, -2]


2596      *
2597      * @return a numerically equal {@code BigDecimal} with any
2598      * trailing zeros removed.
2599      * @since 1.5
2600      */
2601     public BigDecimal stripTrailingZeros() {
2602         if(intCompact!=INFLATED) {


2603             return createAndStripZerosToMatchScale(intCompact, scale, Long.MIN_VALUE);
2604         } else {
2605             return createAndStripZerosToMatchScale(intVal, scale, Long.MIN_VALUE);
2606         }
2607     }
2608 
2609     // Comparison Operations
2610 
2611     /**
2612      * Compares this {@code BigDecimal} with the specified
2613      * {@code BigDecimal}.  Two {@code BigDecimal} objects that are
2614      * equal in value but have a different scale (like 2.0 and 2.00)
2615      * are considered equal by this method.  This method is provided
2616      * in preference to individual methods for each of the six boolean
2617      * comparison operators ({@literal <}, ==,
2618      * {@literal >}, {@literal >=}, !=, {@literal <=}).  The
2619      * suggested idiom for performing these comparisons is:
2620      * {@code (x.compareTo(y)} &lt;<i>op</i>&gt; {@code 0)}, where
2621      * &lt;<i>op</i>&gt; is one of the six comparison operators.
2622      *




2575      * @param n the exponent power of ten to scale by
2576      * @return a BigDecimal whose numerical value is equal to
2577      * ({@code this} * 10<sup>n</sup>)
2578      * @throws ArithmeticException if the scale would be
2579      *         outside the range of a 32-bit integer.
2580      *
2581      * @since 1.5
2582      */
2583     public BigDecimal scaleByPowerOfTen(int n) {
2584         return new BigDecimal(intVal, intCompact,
2585                               checkScale((long)scale - n), precision);
2586     }
2587 
2588     /**
2589      * Returns a {@code BigDecimal} which is numerically equal to
2590      * this one but with any trailing zeros removed from the
2591      * representation.  For example, stripping the trailing zeros from
2592      * the {@code BigDecimal} value {@code 600.0}, which has
2593      * [{@code BigInteger}, {@code scale}] components equals to
2594      * [6000, 1], yields {@code 6E2} with [{@code BigInteger},
2595      * {@code scale}] components equals to [6, -2].  If
2596      * this BigDecimal is numerically equal to zero, then
2597      * {@code BigDecimal.ZERO} is returned.
2598      *
2599      * @return a numerically equal {@code BigDecimal} with any
2600      * trailing zeros removed.
2601      * @since 1.5
2602      */
2603     public BigDecimal stripTrailingZeros() {
2604         if (intCompact == 0 || (intVal != null && intVal.signum() == 0)) {
2605             return BigDecimal.ZERO;
2606         } else if (intCompact != INFLATED) {
2607             return createAndStripZerosToMatchScale(intCompact, scale, Long.MIN_VALUE);
2608         } else {
2609             return createAndStripZerosToMatchScale(intVal, scale, Long.MIN_VALUE);
2610         }
2611     }
2612 
2613     // Comparison Operations
2614 
2615     /**
2616      * Compares this {@code BigDecimal} with the specified
2617      * {@code BigDecimal}.  Two {@code BigDecimal} objects that are
2618      * equal in value but have a different scale (like 2.0 and 2.00)
2619      * are considered equal by this method.  This method is provided
2620      * in preference to individual methods for each of the six boolean
2621      * comparison operators ({@literal <}, ==,
2622      * {@literal >}, {@literal >=}, !=, {@literal <=}).  The
2623      * suggested idiom for performing these comparisons is:
2624      * {@code (x.compareTo(y)} &lt;<i>op</i>&gt; {@code 0)}, where
2625      * &lt;<i>op</i>&gt; is one of the six comparison operators.
2626      *