1519 if ((multiplicand.intCompact != INFLATED)) {
1520 return multiplyAndRound(this.intCompact, multiplicand.intCompact, productScale, mc);
1521 } else {
1522 return multiplyAndRound(this.intCompact, multiplicand.intVal, productScale, mc);
1523 }
1524 } else {
1525 if ((multiplicand.intCompact != INFLATED)) {
1526 return multiplyAndRound(multiplicand.intCompact, this.intVal, productScale, mc);
1527 } else {
1528 return multiplyAndRound(this.intVal, multiplicand.intVal, productScale, mc);
1529 }
1530 }
1531 }
1532
1533 /**
1534 * Returns a {@code BigDecimal} whose value is {@code (this /
1535 * divisor)}, and whose scale is as specified. If rounding must
1536 * be performed to generate a result with the specified scale, the
1537 * specified rounding mode is applied.
1538 *
1539 * <p>The new {@link #divide(BigDecimal, int, RoundingMode)} method
1540 * should be used in preference to this legacy method.
1541 *
1542 * @param divisor value by which this {@code BigDecimal} is to be divided.
1543 * @param scale scale of the {@code BigDecimal} quotient to be returned.
1544 * @param roundingMode rounding mode to apply.
1545 * @return {@code this / divisor}
1546 * @throws ArithmeticException if {@code divisor} is zero,
1547 * {@code roundingMode==ROUND_UNNECESSARY} and
1548 * the specified scale is insufficient to represent the result
1549 * of the division exactly.
1550 * @throws IllegalArgumentException if {@code roundingMode} does not
1551 * represent a valid rounding mode.
1552 * @see #ROUND_UP
1553 * @see #ROUND_DOWN
1554 * @see #ROUND_CEILING
1555 * @see #ROUND_FLOOR
1556 * @see #ROUND_HALF_UP
1557 * @see #ROUND_HALF_DOWN
1558 * @see #ROUND_HALF_EVEN
1559 * @see #ROUND_UNNECESSARY
1560 */
1561 public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) {
1562 if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
1563 throw new IllegalArgumentException("Invalid rounding mode");
1564 if (this.intCompact != INFLATED) {
1565 if ((divisor.intCompact != INFLATED)) {
1566 return divide(this.intCompact, this.scale, divisor.intCompact, divisor.scale, scale, roundingMode);
1567 } else {
1568 return divide(this.intCompact, this.scale, divisor.intVal, divisor.scale, scale, roundingMode);
1569 }
1570 } else {
1571 if ((divisor.intCompact != INFLATED)) {
1572 return divide(this.intVal, this.scale, divisor.intCompact, divisor.scale, scale, roundingMode);
1573 } else {
1574 return divide(this.intVal, this.scale, divisor.intVal, divisor.scale, scale, roundingMode);
1575 }
1576 }
1577 }
1578
1579 /**
1580 * Returns a {@code BigDecimal} whose value is {@code (this /
1585 * @param divisor value by which this {@code BigDecimal} is to be divided.
1586 * @param scale scale of the {@code BigDecimal} quotient to be returned.
1587 * @param roundingMode rounding mode to apply.
1588 * @return {@code this / divisor}
1589 * @throws ArithmeticException if {@code divisor} is zero,
1590 * {@code roundingMode==RoundingMode.UNNECESSARY} and
1591 * the specified scale is insufficient to represent the result
1592 * of the division exactly.
1593 * @since 1.5
1594 */
1595 public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) {
1596 return divide(divisor, scale, roundingMode.oldMode);
1597 }
1598
1599 /**
1600 * Returns a {@code BigDecimal} whose value is {@code (this /
1601 * divisor)}, and whose scale is {@code this.scale()}. If
1602 * rounding must be performed to generate a result with the given
1603 * scale, the specified rounding mode is applied.
1604 *
1605 * <p>The new {@link #divide(BigDecimal, RoundingMode)} method
1606 * should be used in preference to this legacy method.
1607 *
1608 * @param divisor value by which this {@code BigDecimal} is to be divided.
1609 * @param roundingMode rounding mode to apply.
1610 * @return {@code this / divisor}
1611 * @throws ArithmeticException if {@code divisor==0}, or
1612 * {@code roundingMode==ROUND_UNNECESSARY} and
1613 * {@code this.scale()} is insufficient to represent the result
1614 * of the division exactly.
1615 * @throws IllegalArgumentException if {@code roundingMode} does not
1616 * represent a valid rounding mode.
1617 * @see #ROUND_UP
1618 * @see #ROUND_DOWN
1619 * @see #ROUND_CEILING
1620 * @see #ROUND_FLOOR
1621 * @see #ROUND_HALF_UP
1622 * @see #ROUND_HALF_DOWN
1623 * @see #ROUND_HALF_EVEN
1624 * @see #ROUND_UNNECESSARY
1625 */
1626 public BigDecimal divide(BigDecimal divisor, int roundingMode) {
1627 return this.divide(divisor, scale, roundingMode);
1628 }
1629
1630 /**
1631 * Returns a {@code BigDecimal} whose value is {@code (this /
1632 * divisor)}, and whose scale is {@code this.scale()}. If
1633 * rounding must be performed to generate a result with the given
1634 * scale, the specified rounding mode is applied.
1635 *
1636 * @param divisor value by which this {@code BigDecimal} is to be divided.
1637 * @param roundingMode rounding mode to apply.
1638 * @return {@code this / divisor}
1639 * @throws ArithmeticException if {@code divisor==0}, or
1640 * {@code roundingMode==RoundingMode.UNNECESSARY} and
1641 * {@code this.scale()} is insufficient to represent the result
1642 * of the division exactly.
1643 * @since 1.5
1644 */
1645 public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) {
2250
2251
2252 /**
2253 * Returns a {@code BigInteger} whose value is the <i>unscaled
2254 * value</i> of this {@code BigDecimal}. (Computes <code>(this *
2255 * 10<sup>this.scale()</sup>)</code>.)
2256 *
2257 * @return the unscaled value of this {@code BigDecimal}.
2258 * @since 1.2
2259 */
2260 public BigInteger unscaledValue() {
2261 return this.inflated();
2262 }
2263
2264 // Rounding Modes
2265
2266 /**
2267 * Rounding mode to round away from zero. Always increments the
2268 * digit prior to a nonzero discarded fraction. Note that this rounding
2269 * mode never decreases the magnitude of the calculated value.
2270 */
2271 public static final int ROUND_UP = 0;
2272
2273 /**
2274 * Rounding mode to round towards zero. Never increments the digit
2275 * prior to a discarded fraction (i.e., truncates). Note that this
2276 * rounding mode never increases the magnitude of the calculated value.
2277 */
2278 public static final int ROUND_DOWN = 1;
2279
2280 /**
2281 * Rounding mode to round towards positive infinity. If the
2282 * {@code BigDecimal} is positive, behaves as for
2283 * {@code ROUND_UP}; if negative, behaves as for
2284 * {@code ROUND_DOWN}. Note that this rounding mode never
2285 * decreases the calculated value.
2286 */
2287 public static final int ROUND_CEILING = 2;
2288
2289 /**
2290 * Rounding mode to round towards negative infinity. If the
2291 * {@code BigDecimal} is positive, behave as for
2292 * {@code ROUND_DOWN}; if negative, behave as for
2293 * {@code ROUND_UP}. Note that this rounding mode never
2294 * increases the calculated value.
2295 */
2296 public static final int ROUND_FLOOR = 3;
2297
2298 /**
2299 * Rounding mode to round towards {@literal "nearest neighbor"}
2300 * unless both neighbors are equidistant, in which case round up.
2301 * Behaves as for {@code ROUND_UP} if the discarded fraction is
2302 * ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
2303 * that this is the rounding mode that most of us were taught in
2304 * grade school.
2305 */
2306 public static final int ROUND_HALF_UP = 4;
2307
2308 /**
2309 * Rounding mode to round towards {@literal "nearest neighbor"}
2310 * unless both neighbors are equidistant, in which case round
2311 * down. Behaves as for {@code ROUND_UP} if the discarded
2312 * fraction is {@literal >} 0.5; otherwise, behaves as for
2313 * {@code ROUND_DOWN}.
2314 */
2315 public static final int ROUND_HALF_DOWN = 5;
2316
2317 /**
2318 * Rounding mode to round towards the {@literal "nearest neighbor"}
2319 * unless both neighbors are equidistant, in which case, round
2320 * towards the even neighbor. Behaves as for
2321 * {@code ROUND_HALF_UP} if the digit to the left of the
2322 * discarded fraction is odd; behaves as for
2323 * {@code ROUND_HALF_DOWN} if it's even. Note that this is the
2324 * rounding mode that minimizes cumulative error when applied
2325 * repeatedly over a sequence of calculations.
2326 */
2327 public static final int ROUND_HALF_EVEN = 6;
2328
2329 /**
2330 * Rounding mode to assert that the requested operation has an exact
2331 * result, hence no rounding is necessary. If this rounding mode is
2332 * specified on an operation that yields an inexact result, an
2333 * {@code ArithmeticException} is thrown.
2334 */
2335 public static final int ROUND_UNNECESSARY = 7;
2336
2337
2338 // Scaling/Rounding Operations
2339
2340 /**
2341 * Returns a {@code BigDecimal} rounded according to the
2342 * {@code MathContext} settings. If the precision setting is 0 then
2343 * no rounding takes place.
2344 *
2345 * <p>The effect of this method is identical to that of the
2346 * {@link #plus(MathContext)} method.
2347 *
2348 * @param mc the context to use.
2349 * @return a {@code BigDecimal} rounded according to the
2350 * {@code MathContext} settings.
2351 * @throws ArithmeticException if the rounding mode is
2352 * {@code UNNECESSARY} and the
2353 * {@code BigDecimal} operation would require rounding.
2354 * @see #plus(MathContext)
2391 return setScale(newScale, roundingMode.oldMode);
2392 }
2393
2394 /**
2395 * Returns a {@code BigDecimal} whose scale is the specified
2396 * value, and whose unscaled value is determined by multiplying or
2397 * dividing this {@code BigDecimal}'s unscaled value by the
2398 * appropriate power of ten to maintain its overall value. If the
2399 * scale is reduced by the operation, the unscaled value must be
2400 * divided (rather than multiplied), and the value may be changed;
2401 * in this case, the specified rounding mode is applied to the
2402 * division.
2403 *
2404 * <p>Note that since BigDecimal objects are immutable, calls of
2405 * this method do <i>not</i> result in the original object being
2406 * modified, contrary to the usual convention of having methods
2407 * named <code>set<i>X</i></code> mutate field <i>{@code X}</i>.
2408 * Instead, {@code setScale} returns an object with the proper
2409 * scale; the returned object may or may not be newly allocated.
2410 *
2411 * <p>The new {@link #setScale(int, RoundingMode)} method should
2412 * be used in preference to this legacy method.
2413 *
2414 * @param newScale scale of the {@code BigDecimal} value to be returned.
2415 * @param roundingMode The rounding mode to apply.
2416 * @return a {@code BigDecimal} whose scale is the specified value,
2417 * and whose unscaled value is determined by multiplying or
2418 * dividing this {@code BigDecimal}'s unscaled value by the
2419 * appropriate power of ten to maintain its overall value.
2420 * @throws ArithmeticException if {@code roundingMode==ROUND_UNNECESSARY}
2421 * and the specified scaling operation would require
2422 * rounding.
2423 * @throws IllegalArgumentException if {@code roundingMode} does not
2424 * represent a valid rounding mode.
2425 * @see #ROUND_UP
2426 * @see #ROUND_DOWN
2427 * @see #ROUND_CEILING
2428 * @see #ROUND_FLOOR
2429 * @see #ROUND_HALF_UP
2430 * @see #ROUND_HALF_DOWN
2431 * @see #ROUND_HALF_EVEN
2432 * @see #ROUND_UNNECESSARY
2433 */
2434 public BigDecimal setScale(int newScale, int roundingMode) {
2435 if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
2436 throw new IllegalArgumentException("Invalid rounding mode");
2437
2438 int oldScale = this.scale;
2439 if (newScale == oldScale) // easy case
2440 return this;
2441 if (this.signum() == 0) // zero can have any scale
2442 return zeroValueOf(newScale);
2443 if(this.intCompact!=INFLATED) {
2444 long rs = this.intCompact;
2445 if (newScale > oldScale) {
2446 int raise = checkScale((long) newScale - oldScale);
2447 if ((rs = longMultiplyPowerTen(rs, raise)) != INFLATED) {
2448 return valueOf(rs,newScale);
2449 }
2450 BigInteger rb = bigMultiplyPowerTen(raise);
2451 return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0);
2452 } else {
2453 // newScale < oldScale -- drop some digits
|
1519 if ((multiplicand.intCompact != INFLATED)) {
1520 return multiplyAndRound(this.intCompact, multiplicand.intCompact, productScale, mc);
1521 } else {
1522 return multiplyAndRound(this.intCompact, multiplicand.intVal, productScale, mc);
1523 }
1524 } else {
1525 if ((multiplicand.intCompact != INFLATED)) {
1526 return multiplyAndRound(multiplicand.intCompact, this.intVal, productScale, mc);
1527 } else {
1528 return multiplyAndRound(this.intVal, multiplicand.intVal, productScale, mc);
1529 }
1530 }
1531 }
1532
1533 /**
1534 * Returns a {@code BigDecimal} whose value is {@code (this /
1535 * divisor)}, and whose scale is as specified. If rounding must
1536 * be performed to generate a result with the specified scale, the
1537 * specified rounding mode is applied.
1538 *
1539 * @deprecated The method {@link #divide(BigDecimal, int, RoundingMode)}
1540 * should be used in preference to this legacy method.
1541 *
1542 * @param divisor value by which this {@code BigDecimal} is to be divided.
1543 * @param scale scale of the {@code BigDecimal} quotient to be returned.
1544 * @param roundingMode rounding mode to apply.
1545 * @return {@code this / divisor}
1546 * @throws ArithmeticException if {@code divisor} is zero,
1547 * {@code roundingMode==ROUND_UNNECESSARY} and
1548 * the specified scale is insufficient to represent the result
1549 * of the division exactly.
1550 * @throws IllegalArgumentException if {@code roundingMode} does not
1551 * represent a valid rounding mode.
1552 * @see #ROUND_UP
1553 * @see #ROUND_DOWN
1554 * @see #ROUND_CEILING
1555 * @see #ROUND_FLOOR
1556 * @see #ROUND_HALF_UP
1557 * @see #ROUND_HALF_DOWN
1558 * @see #ROUND_HALF_EVEN
1559 * @see #ROUND_UNNECESSARY
1560 */
1561 @Deprecated(since="9")
1562 public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) {
1563 if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
1564 throw new IllegalArgumentException("Invalid rounding mode");
1565 if (this.intCompact != INFLATED) {
1566 if ((divisor.intCompact != INFLATED)) {
1567 return divide(this.intCompact, this.scale, divisor.intCompact, divisor.scale, scale, roundingMode);
1568 } else {
1569 return divide(this.intCompact, this.scale, divisor.intVal, divisor.scale, scale, roundingMode);
1570 }
1571 } else {
1572 if ((divisor.intCompact != INFLATED)) {
1573 return divide(this.intVal, this.scale, divisor.intCompact, divisor.scale, scale, roundingMode);
1574 } else {
1575 return divide(this.intVal, this.scale, divisor.intVal, divisor.scale, scale, roundingMode);
1576 }
1577 }
1578 }
1579
1580 /**
1581 * Returns a {@code BigDecimal} whose value is {@code (this /
1586 * @param divisor value by which this {@code BigDecimal} is to be divided.
1587 * @param scale scale of the {@code BigDecimal} quotient to be returned.
1588 * @param roundingMode rounding mode to apply.
1589 * @return {@code this / divisor}
1590 * @throws ArithmeticException if {@code divisor} is zero,
1591 * {@code roundingMode==RoundingMode.UNNECESSARY} and
1592 * the specified scale is insufficient to represent the result
1593 * of the division exactly.
1594 * @since 1.5
1595 */
1596 public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) {
1597 return divide(divisor, scale, roundingMode.oldMode);
1598 }
1599
1600 /**
1601 * Returns a {@code BigDecimal} whose value is {@code (this /
1602 * divisor)}, and whose scale is {@code this.scale()}. If
1603 * rounding must be performed to generate a result with the given
1604 * scale, the specified rounding mode is applied.
1605 *
1606 * @deprecated The method {@link #divide(BigDecimal, RoundingMode)}
1607 * should be used in preference to this legacy method.
1608 *
1609 * @param divisor value by which this {@code BigDecimal} is to be divided.
1610 * @param roundingMode rounding mode to apply.
1611 * @return {@code this / divisor}
1612 * @throws ArithmeticException if {@code divisor==0}, or
1613 * {@code roundingMode==ROUND_UNNECESSARY} and
1614 * {@code this.scale()} is insufficient to represent the result
1615 * of the division exactly.
1616 * @throws IllegalArgumentException if {@code roundingMode} does not
1617 * represent a valid rounding mode.
1618 * @see #ROUND_UP
1619 * @see #ROUND_DOWN
1620 * @see #ROUND_CEILING
1621 * @see #ROUND_FLOOR
1622 * @see #ROUND_HALF_UP
1623 * @see #ROUND_HALF_DOWN
1624 * @see #ROUND_HALF_EVEN
1625 * @see #ROUND_UNNECESSARY
1626 */
1627 @Deprecated(since="9")
1628 public BigDecimal divide(BigDecimal divisor, int roundingMode) {
1629 return this.divide(divisor, scale, roundingMode);
1630 }
1631
1632 /**
1633 * Returns a {@code BigDecimal} whose value is {@code (this /
1634 * divisor)}, and whose scale is {@code this.scale()}. If
1635 * rounding must be performed to generate a result with the given
1636 * scale, the specified rounding mode is applied.
1637 *
1638 * @param divisor value by which this {@code BigDecimal} is to be divided.
1639 * @param roundingMode rounding mode to apply.
1640 * @return {@code this / divisor}
1641 * @throws ArithmeticException if {@code divisor==0}, or
1642 * {@code roundingMode==RoundingMode.UNNECESSARY} and
1643 * {@code this.scale()} is insufficient to represent the result
1644 * of the division exactly.
1645 * @since 1.5
1646 */
1647 public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) {
2252
2253
2254 /**
2255 * Returns a {@code BigInteger} whose value is the <i>unscaled
2256 * value</i> of this {@code BigDecimal}. (Computes <code>(this *
2257 * 10<sup>this.scale()</sup>)</code>.)
2258 *
2259 * @return the unscaled value of this {@code BigDecimal}.
2260 * @since 1.2
2261 */
2262 public BigInteger unscaledValue() {
2263 return this.inflated();
2264 }
2265
2266 // Rounding Modes
2267
2268 /**
2269 * Rounding mode to round away from zero. Always increments the
2270 * digit prior to a nonzero discarded fraction. Note that this rounding
2271 * mode never decreases the magnitude of the calculated value.
2272 *
2273 * @deprecated Use {@link RoundingMode#UP} instead.
2274 */
2275 @Deprecated(since="9")
2276 public static final int ROUND_UP = 0;
2277
2278 /**
2279 * Rounding mode to round towards zero. Never increments the digit
2280 * prior to a discarded fraction (i.e., truncates). Note that this
2281 * rounding mode never increases the magnitude of the calculated value.
2282 *
2283 * @deprecated Use {@link RoundingMode#DOWN} instead.
2284 */
2285 @Deprecated(since="9")
2286 public static final int ROUND_DOWN = 1;
2287
2288 /**
2289 * Rounding mode to round towards positive infinity. If the
2290 * {@code BigDecimal} is positive, behaves as for
2291 * {@code ROUND_UP}; if negative, behaves as for
2292 * {@code ROUND_DOWN}. Note that this rounding mode never
2293 * decreases the calculated value.
2294 *
2295 * @deprecated Use {@link RoundingMode#CEILING} instead.
2296 */
2297 @Deprecated(since="9")
2298 public static final int ROUND_CEILING = 2;
2299
2300 /**
2301 * Rounding mode to round towards negative infinity. If the
2302 * {@code BigDecimal} is positive, behave as for
2303 * {@code ROUND_DOWN}; if negative, behave as for
2304 * {@code ROUND_UP}. Note that this rounding mode never
2305 * increases the calculated value.
2306 *
2307 * @deprecated Use {@link RoundingMode#FLOOR} instead.
2308 */
2309 @Deprecated(since="9")
2310 public static final int ROUND_FLOOR = 3;
2311
2312 /**
2313 * Rounding mode to round towards {@literal "nearest neighbor"}
2314 * unless both neighbors are equidistant, in which case round up.
2315 * Behaves as for {@code ROUND_UP} if the discarded fraction is
2316 * ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
2317 * that this is the rounding mode that most of us were taught in
2318 * grade school.
2319 *
2320 * @deprecated Use {@link RoundingMode#HALF_UP} instead.
2321 */
2322 @Deprecated(since="9")
2323 public static final int ROUND_HALF_UP = 4;
2324
2325 /**
2326 * Rounding mode to round towards {@literal "nearest neighbor"}
2327 * unless both neighbors are equidistant, in which case round
2328 * down. Behaves as for {@code ROUND_UP} if the discarded
2329 * fraction is {@literal >} 0.5; otherwise, behaves as for
2330 * {@code ROUND_DOWN}.
2331 *
2332 * @deprecated Use {@link RoundingMode#HALF_DOWN} instead.
2333 */
2334 @Deprecated(since="9")
2335 public static final int ROUND_HALF_DOWN = 5;
2336
2337 /**
2338 * Rounding mode to round towards the {@literal "nearest neighbor"}
2339 * unless both neighbors are equidistant, in which case, round
2340 * towards the even neighbor. Behaves as for
2341 * {@code ROUND_HALF_UP} if the digit to the left of the
2342 * discarded fraction is odd; behaves as for
2343 * {@code ROUND_HALF_DOWN} if it's even. Note that this is the
2344 * rounding mode that minimizes cumulative error when applied
2345 * repeatedly over a sequence of calculations.
2346 *
2347 * @deprecated Use {@link RoundingMode#HALF_EVEN} instead.
2348 */
2349 @Deprecated(since="9")
2350 public static final int ROUND_HALF_EVEN = 6;
2351
2352 /**
2353 * Rounding mode to assert that the requested operation has an exact
2354 * result, hence no rounding is necessary. If this rounding mode is
2355 * specified on an operation that yields an inexact result, an
2356 * {@code ArithmeticException} is thrown.
2357 *
2358 * @deprecated Use {@link RoundingMode#UNNECESSARY} instead.
2359 */
2360 @Deprecated(since="9")
2361 public static final int ROUND_UNNECESSARY = 7;
2362
2363
2364 // Scaling/Rounding Operations
2365
2366 /**
2367 * Returns a {@code BigDecimal} rounded according to the
2368 * {@code MathContext} settings. If the precision setting is 0 then
2369 * no rounding takes place.
2370 *
2371 * <p>The effect of this method is identical to that of the
2372 * {@link #plus(MathContext)} method.
2373 *
2374 * @param mc the context to use.
2375 * @return a {@code BigDecimal} rounded according to the
2376 * {@code MathContext} settings.
2377 * @throws ArithmeticException if the rounding mode is
2378 * {@code UNNECESSARY} and the
2379 * {@code BigDecimal} operation would require rounding.
2380 * @see #plus(MathContext)
2417 return setScale(newScale, roundingMode.oldMode);
2418 }
2419
2420 /**
2421 * Returns a {@code BigDecimal} whose scale is the specified
2422 * value, and whose unscaled value is determined by multiplying or
2423 * dividing this {@code BigDecimal}'s unscaled value by the
2424 * appropriate power of ten to maintain its overall value. If the
2425 * scale is reduced by the operation, the unscaled value must be
2426 * divided (rather than multiplied), and the value may be changed;
2427 * in this case, the specified rounding mode is applied to the
2428 * division.
2429 *
2430 * <p>Note that since BigDecimal objects are immutable, calls of
2431 * this method do <i>not</i> result in the original object being
2432 * modified, contrary to the usual convention of having methods
2433 * named <code>set<i>X</i></code> mutate field <i>{@code X}</i>.
2434 * Instead, {@code setScale} returns an object with the proper
2435 * scale; the returned object may or may not be newly allocated.
2436 *
2437 * @deprecated The method {@link #setScale(int, RoundingMode)} should
2438 * be used in preference to this legacy method.
2439 *
2440 * @param newScale scale of the {@code BigDecimal} value to be returned.
2441 * @param roundingMode The rounding mode to apply.
2442 * @return a {@code BigDecimal} whose scale is the specified value,
2443 * and whose unscaled value is determined by multiplying or
2444 * dividing this {@code BigDecimal}'s unscaled value by the
2445 * appropriate power of ten to maintain its overall value.
2446 * @throws ArithmeticException if {@code roundingMode==ROUND_UNNECESSARY}
2447 * and the specified scaling operation would require
2448 * rounding.
2449 * @throws IllegalArgumentException if {@code roundingMode} does not
2450 * represent a valid rounding mode.
2451 * @see #ROUND_UP
2452 * @see #ROUND_DOWN
2453 * @see #ROUND_CEILING
2454 * @see #ROUND_FLOOR
2455 * @see #ROUND_HALF_UP
2456 * @see #ROUND_HALF_DOWN
2457 * @see #ROUND_HALF_EVEN
2458 * @see #ROUND_UNNECESSARY
2459 */
2460 @Deprecated(since="9")
2461 public BigDecimal setScale(int newScale, int roundingMode) {
2462 if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
2463 throw new IllegalArgumentException("Invalid rounding mode");
2464
2465 int oldScale = this.scale;
2466 if (newScale == oldScale) // easy case
2467 return this;
2468 if (this.signum() == 0) // zero can have any scale
2469 return zeroValueOf(newScale);
2470 if(this.intCompact!=INFLATED) {
2471 long rs = this.intCompact;
2472 if (newScale > oldScale) {
2473 int raise = checkScale((long) newScale - oldScale);
2474 if ((rs = longMultiplyPowerTen(rs, raise)) != INFLATED) {
2475 return valueOf(rs,newScale);
2476 }
2477 BigInteger rb = bigMultiplyPowerTen(raise);
2478 return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0);
2479 } else {
2480 // newScale < oldScale -- drop some digits
|