diff options
Diffstat (limited to 'java/math/BigDecimal.java')
-rw-r--r-- | java/math/BigDecimal.java | 67 |
1 files changed, 55 insertions, 12 deletions
diff --git a/java/math/BigDecimal.java b/java/math/BigDecimal.java index bca9b12c2..28c4d45d3 100644 --- a/java/math/BigDecimal.java +++ b/java/math/BigDecimal.java @@ -37,7 +37,7 @@ exception statement from your version. */ package java.math; -public class BigDecimal extends Number implements Comparable +public class BigDecimal extends Number implements Comparable<BigDecimal> { private BigInteger intVal; private int scale; @@ -672,7 +672,38 @@ public class BigDecimal extends Number implements Comparable { return divide (val, scale, roundingMode); } - + + /** + * Returns a BigDecimal whose value is (this / val), with the specified scale + * and rounding according to the RoundingMode + * @param val the divisor + * @param scale the scale of the BigDecimal returned + * @param roundingMode the rounding mode to use + * @return a BigDecimal whose value is approximately (this / val) + * @throws ArithmeticException if divisor is zero or the rounding mode is + * UNNECESSARY but the specified scale cannot represent the value exactly + * @since 1.5 + */ + public BigDecimal divide(BigDecimal val, + int scale, RoundingMode roundingMode) + { + return divide (val, scale, roundingMode.ordinal()); + } + + /** + * Returns a BigDecimal whose value is (this / val) rounded according to the + * RoundingMode + * @param val the divisor + * @param roundingMode the rounding mode to use + * @return a BigDecimal whose value is approximately (this / val) + * @throws ArithmeticException if divisor is zero or the rounding mode is + * UNNECESSARY but the specified scale cannot represent the value exactly + */ + public BigDecimal divide (BigDecimal val, RoundingMode roundingMode) + { + return divide (val, scale, roundingMode.ordinal()); + } + public BigDecimal divide(BigDecimal val, int newScale, int roundingMode) throws ArithmeticException, IllegalArgumentException { @@ -823,12 +854,7 @@ public class BigDecimal extends Number implements Comparable return this; } - public int compareTo (Object obj) - { - return compareTo((BigDecimal) obj); - } - - public int compareTo (BigDecimal val) + public int compareTo (BigDecimal val) { if (scale == val.scale) return intVal.compareTo (val.intVal); @@ -973,7 +999,7 @@ public class BigDecimal extends Number implements Comparable { return round(mc); } - + /** * Returns a BigDecimal which is this BigDecimal rounded according to the * MathContext rounding settings. @@ -993,12 +1019,12 @@ public class BigDecimal extends Number implements Comparable // Make a new BigDecimal which is the correct power of 10 to chop off // the required number of digits and then call divide. BigDecimal div = new BigDecimal(BigInteger.TEN.pow(numToChop)); - BigDecimal rounded = divide(div, scale, 4); + BigDecimal rounded = divide(div, scale, mc.getRoundingMode().ordinal()); rounded.scale -= numToChop; rounded.precision = mcPrecision; return rounded; } - + /** * Returns the precision of this BigDecimal (the number of digits in the * unscaled value). The precision of a zero value is 1. @@ -1350,7 +1376,24 @@ public class BigDecimal extends Number implements Comparable if( scale < 0 ) throw new ArithmeticException("Scale parameter < 0."); return divide (ONE, scale, roundingMode); } - + + /** + * Returns a BigDecimal whose value is the same as this BigDecimal but whose + * representation has a scale of <code>newScale</code>. If the scale is + * reduced then rounding may occur, according to the RoundingMode. + * @param newScale + * @param roundingMode + * @return a BigDecimal whose scale is as given, whose value is + * <code>this</code> with possible rounding + * @throws ArithmeticException if the rounding mode is UNNECESSARY but + * rounding is required + * @since 1.5 + */ + public BigDecimal setScale(int newScale, RoundingMode roundingMode) + { + return setScale(newScale, roundingMode.ordinal()); + } + /** * Returns a new BigDecimal constructed from the BigDecimal(String) * constructor using the Double.toString(double) method to obtain |