summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Balkissoon <abalkiss@redhat.com>2006-02-24 21:48:05 +0000
committerAnthony Balkissoon <abalkiss@redhat.com>2006-02-24 21:48:05 +0000
commit710b04d41fbfd69cb7c0ca5faf2a6abf880ef4ce (patch)
tree079085d4ebb57fbc9150a778068016fb0c07d81f
parent049b5f65015be5c0c774ecd1542a60317b5d9297 (diff)
downloadclasspath-710b04d41fbfd69cb7c0ca5faf2a6abf880ef4ce.tar.gz
2006-02-24 Anthony Balkissoon <abalkiss@redhat.com>
* java/math/BigDecimal.java: (BigDecimal(long, MathContext)): New constructor. (BigDecimal(BigInteger, MathContext)): Likewise. (BigDecimal(String, MathContext)): Likewise. (BigDecimal(double, MathContext)): Likewise. (round): Fixed a typo where the precision field was used instead of a call to the precision method, and also store the new precision in the returned BigDecimal. (abs(MathContext)): New method.
-rw-r--r--ChangeLog12
-rw-r--r--java/math/BigDecimal.java95
2 files changed, 105 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 16f4b9c56..1c817c31b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
2006-02-24 Anthony Balkissoon <abalkiss@redhat.com>
+ * java/math/BigDecimal.java:
+ (BigDecimal(long, MathContext)): New constructor.
+ (BigDecimal(BigInteger, MathContext)): Likewise.
+ (BigDecimal(String, MathContext)): Likewise.
+ (BigDecimal(double, MathContext)): Likewise.
+ (round): Fixed a typo where the precision field was used instead of a
+ call to the precision method, and also store the new precision in the
+ returned BigDecimal.
+ (abs(MathContext)): New method.
+
+2006-02-24 Anthony Balkissoon <abalkiss@redhat.com>
+
* java/math/BigDecimal.java
(toBigInteger): Fixed problem where this method couldn't handle
negative values for scale.
diff --git a/java/math/BigDecimal.java b/java/math/BigDecimal.java
index b1b7aaae2..2cdfdebac 100644
--- a/java/math/BigDecimal.java
+++ b/java/math/BigDecimal.java
@@ -99,6 +99,66 @@ public class BigDecimal extends Number implements Comparable<BigDecimal>
}
/**
+ * Constructs a BigDecimal from the long in the same way as BigDecimal(long)
+ * and then rounds according to the MathContext.
+ * @param val the long from which we create the initial BigDecimal
+ * @param mc the MathContext that specifies the rounding behaviour
+ * @since 1.5
+ */
+ public BigDecimal (long val, MathContext mc)
+ {
+ this(val);
+ if (mc.getPrecision() != 0)
+ {
+ BigDecimal result = this.round(mc);
+ this.intVal = result.intVal;
+ this.scale = result.scale;
+ this.precision = result.precision;
+ }
+ }
+
+ /**
+ * Constructs a BigDecimal whose value is given by num rounded according to
+ * mc. Since num is already a BigInteger, the rounding refers only to the
+ * precision setting in mc, if mc.getPrecision() returns an int lower than
+ * the number of digits in num, then rounding is necessary.
+ * @param num the unscaledValue, before rounding
+ * @param mc the MathContext that specifies the precision
+ * @since 1.5
+ */
+ public BigDecimal (BigInteger num, MathContext mc)
+ {
+ this (num, 0);
+ if (mc.getPrecision() != 0)
+ {
+ BigDecimal result = this.round(mc);
+ this.intVal = result.intVal;
+ this.scale = result.scale;
+ this.precision = result.precision;
+ }
+ }
+
+ /**
+ * Constructs a BigDecimal from the String val according to the same
+ * rules as the BigDecimal(String) constructor and then rounds
+ * according to the MathContext mc.
+ * @param val the String from which we construct the initial BigDecimal
+ * @param mc the MathContext that specifies the rounding
+ * @since 1.5
+ */
+ public BigDecimal (String val, MathContext mc)
+ {
+ this (val);
+ if (mc.getPrecision() != 0)
+ {
+ BigDecimal result = this.round(mc);
+ this.intVal = result.intVal;
+ this.scale = result.scale;
+ this.precision = result.precision;
+ }
+ }
+
+ /**
* Constructs a BigDecimal whose unscaled value is num and whose
* scale is zero.
* @param num the value of the new BigDecimal
@@ -120,6 +180,25 @@ public class BigDecimal extends Number implements Comparable<BigDecimal>
this.scale = scale;
}
+ /**
+ * Constructs a BigDecimal in the same way as BigDecimal(double) and then
+ * rounds according to the MathContext.
+ * @param num the double from which the initial BigDecimal is created
+ * @param mc the MathContext that specifies the rounding behaviour
+ * @since 1.5
+ */
+ public BigDecimal (double num, MathContext mc)
+ {
+ this (num);
+ if (mc.getPrecision() != 0)
+ {
+ BigDecimal result = this.round(mc);
+ this.intVal = result.intVal;
+ this.scale = result.scale;
+ this.precision = result.precision;
+ }
+ }
+
public BigDecimal (double num) throws NumberFormatException
{
if (Double.isInfinite (num) || Double.isNaN (num))
@@ -703,7 +782,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal>
public BigDecimal round(MathContext mc)
{
int mcPrecision = mc.getPrecision();
- int numToChop = precision - mcPrecision;
+ int numToChop = precision() - mcPrecision;
// If mc specifies not to chop any digits or if we've already chopped
// enough digits (say by using a MathContext in the constructor for this
// BigDecimal) then just return this.
@@ -716,6 +795,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal>
new BigDecimal(BigInteger.valueOf((long)Math.pow(10, numToChop)));
BigDecimal rounded = divide(div, scale, mc.getRoundingMode().ordinal());
rounded.scale -= numToChop;
+ rounded.precision = mcPrecision;
return rounded;
}
@@ -1161,5 +1241,16 @@ public class BigDecimal extends Number implements Comparable<BigDecimal>
return result;
}
-
+ /**
+ * Returns a BigDecimal whose value is the absolute value of this BigDecimal
+ * with rounding according to the given MathContext.
+ * @param mc the MathContext
+ * @return the new BigDecimal
+ */
+ public BigDecimal abs(MathContext mc)
+ {
+ BigDecimal result = abs();
+ result = result.round(mc);
+ return result;
+ }
}