From 4e0d7b8e223511e05fd67c6c90a4af369e99787f Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 2 Mar 2011 06:07:18 +0000 Subject: Added an innocuous java BigDecimal -> scala Big... Added an innocuous java BigDecimal -> scala BigDecimal implicit. Cleaned up some documentation. Closes #2199, no review. --- src/library/scala/math/BigDecimal.scala | 108 ++++++++++++++++---------------- 1 file changed, 55 insertions(+), 53 deletions(-) diff --git a/src/library/scala/math/BigDecimal.scala b/src/library/scala/math/BigDecimal.scala index 27de852ed0..9c1c4f45a9 100644 --- a/src/library/scala/math/BigDecimal.scala +++ b/src/library/scala/math/BigDecimal.scala @@ -39,20 +39,20 @@ object BigDecimal { val UP, DOWN, CEILING, FLOOR, HALF_UP, HALF_DOWN, HALF_EVEN, UNNECESSARY = Value } - /** Constructs a BigDecimal using the java BigDecimal static + /** Constructs a `BigDecimal` using the java BigDecimal static * valueOf constructor. * * @param d the specified double value - * @return the constructed BigDecimal + * @return the constructed `BigDecimal` */ def valueOf(d: Double): BigDecimal = apply(BigDec valueOf d) def valueOf(d: Double, mc: MathContext): BigDecimal = apply(BigDec valueOf d, mc) - /** Constructs a BigDecimal whose value is equal to that of the - * specified Integer value. + /** Constructs a `BigDecimal` whose value is equal to that of the + * specified `Integer` value. * * @param i the specified integer value - * @return the constructed BigDecimal + * @return the constructed `BigDecimal` */ def apply(i: Int): BigDecimal = apply(i, defaultMathContext) def apply(i: Int, mc: MathContext): BigDecimal = @@ -64,11 +64,11 @@ object BigDecimal { } else new BigDecimal(BigDec.valueOf(i), mc) - /** Constructs a BigDecimal whose value is equal to that of the + /** Constructs a `BigDecimal` whose value is equal to that of the * specified long value. * * @param l the specified long value - * @return the constructed BigDecimal + * @return the constructed `BigDecimal` */ def apply(l: Long): BigDecimal = if (minCached <= l && l <= maxCached) apply(l.toInt) @@ -77,12 +77,12 @@ object BigDecimal { def apply(l: Long, mc: MathContext): BigDecimal = new BigDecimal(new BigDec(l, mc), mc) - /** Constructs a BigDecimal whose unscaled value is equal to that + /** Constructs a `BigDecimal` whose unscaled value is equal to that * of the specified long value. * * @param unscaledVal the value * @param scale the scale - * @return the constructed BigDecimal + * @return the constructed `BigDecimal` */ def apply(unscaledVal: Long, scale: Int): BigDecimal = apply(BigInt(unscaledVal), scale) @@ -90,11 +90,11 @@ object BigDecimal { def apply(unscaledVal: Long, scale: Int, mc: MathContext): BigDecimal = apply(BigInt(unscaledVal), scale, mc) - /** Constructs a BigDecimal whose value is equal to that of the + /** Constructs a `BigDecimal` whose value is equal to that of the * specified double value. * - * @param d the specified Double value - * @return the constructed BigDecimal + * @param d the specified `Double` value + * @return the constructed `BigDecimal` */ def apply(d: Double): BigDecimal = apply(d, defaultMathContext) // note we don't use the static valueOf because it doesn't let us supply @@ -102,36 +102,36 @@ object BigDecimal { def apply(d: Double, mc: MathContext): BigDecimal = new BigDecimal(new BigDec(jl.Double.toString(d), mc), mc) - /** Translates a character array representation of a BigDecimal - * into a BigDecimal. + /** Translates a character array representation of a `BigDecimal` + * into a `BigDecimal`. */ def apply(x: Array[Char]): BigDecimal = apply(x, defaultMathContext) def apply(x: Array[Char], mc: MathContext): BigDecimal = new BigDecimal(new BigDec(x.mkString, mc), mc) - /** Translates the decimal String representation of a BigDecimal - * into a BigDecimal. + /** Translates the decimal String representation of a `BigDecimal` + * into a `BigDecimal`. */ def apply(x: String): BigDecimal = apply(x, defaultMathContext) def apply(x: String, mc: MathContext): BigDecimal = new BigDecimal(new BigDec(x, mc), mc) - /** Constructs a BigDecimal whose value is equal to that of the - * specified BigInt value. + /** Constructs a `BigDecimal` whose value is equal to that of the + * specified `BigInt` value. * - * @param x the specified BigInt value - * @return the constructed BigDecimal + * @param x the specified `BigInt` value + * @return the constructed `BigDecimal` */ def apply(x: BigInt): BigDecimal = apply(x, defaultMathContext) def apply(x: BigInt, mc: MathContext): BigDecimal = new BigDecimal(new BigDec(x.bigInteger, mc), mc) - /** Constructs a BigDecimal whose unscaled value is equal to that - * of the specified BigInt value. + /** Constructs a `BigDecimal` whose unscaled value is equal to that + * of the specified `BigInt` value. * - * @param unscaledVal the specified BigInt value + * @param unscaledVal the specified `BigInt` value * @param scale the scale - * @return the constructed BigDecimal + * @return the constructed `BigDecimal` */ def apply(unscaledVal: BigInt, scale: Int): BigDecimal = apply(unscaledVal, scale, defaultMathContext) def apply(unscaledVal: BigInt, scale: Int, mc: MathContext): BigDecimal = @@ -140,14 +140,17 @@ object BigDecimal { def apply(bd: BigDec): BigDecimal = apply(bd, defaultMathContext) def apply(bd: BigDec, mc: MathContext): BigDecimal = new BigDecimal(bd, mc) - /** Implicit conversion from Int to BigDecimal. */ + /** Implicit conversion from `Int` to `BigDecimal`. */ implicit def int2bigDecimal(i: Int): BigDecimal = apply(i) - /** Implicit conversion from Long to BigDecimal. */ + /** Implicit conversion from `Long` to `BigDecimal`. */ implicit def long2bigDecimal(l: Long): BigDecimal = apply(l) - /** Implicit conversion from Double to BigDecimal. */ + /** Implicit conversion from `Double` to `BigDecimal`. */ implicit def double2bigDecimal(d: Double): BigDecimal = valueOf(d, defaultMathContext) + + /** Implicit conversion from `java.math.BigDecimal` to `scala.BigDecimal`. */ + implicit def javaBigDecimal2bigDecimal(x: BigDec): BigDecimal = apply(x) } /** @@ -157,8 +160,7 @@ object BigDecimal { class BigDecimal( val bigDecimal: BigDec, val mc: MathContext) -extends ScalaNumber with ScalaNumericConversions with Serializable -{ +extends ScalaNumber with ScalaNumericConversions with Serializable { def this(bigDecimal: BigDec) = this(bigDecimal, BigDecimal.defaultMathContext) import BigDecimal.RoundingMode._ @@ -276,7 +278,7 @@ extends ScalaNumber with ScalaNumericConversions with Serializable */ def signum: Int = this.bigDecimal.signum() - /** Returns the precision of this BigDecimal. + /** Returns the precision of this `BigDecimal`. */ def precision: Int = this.bigDecimal.precision() @@ -284,7 +286,7 @@ extends ScalaNumber with ScalaNumericConversions with Serializable */ def round(mc: MathContext): BigDecimal = this.bigDecimal round mc - /** Returns the scale of this BigDecimal. + /** Returns the scale of this `BigDecimal`. */ def scale: Int = this.bigDecimal.scale() @@ -296,7 +298,7 @@ extends ScalaNumber with ScalaNumericConversions with Serializable */ def apply(mc: MathContext): BigDecimal = BigDecimal(this.bigDecimal.toString, mc) - /** Returns a BigDecimal whose scale is the specified value, and whose value is + /** Returns a `BigDecimal` whose scale is the specified value, and whose value is * numerically equal to this BigDecimal's. */ def setScale(scale: Int): BigDecimal = this.bigDecimal setScale scale @@ -304,28 +306,28 @@ extends ScalaNumber with ScalaNumericConversions with Serializable def setScale(scale: Int, mode: RoundingMode): BigDecimal = this.bigDecimal.setScale(scale, mode.id) - /** Converts this BigDecimal to a byte. - * If the BigDecimal is too big to fit in a byte, only the low-order 8 bits are returned. + /** Converts this BigDecimal to a Byte. + * If the BigDecimal is too big to fit in a Byte, only the low-order 8 bits are returned. * Note that this conversion can lose information about the overall magnitude of the * BigDecimal value as well as return a result with the opposite sign. */ override def byteValue = intValue.toByte - /** Converts this BigDecimal to a short. - * If the BigDecimal is too big to fit in a byte, only the low-order 16 bits are returned. + /** Converts this BigDecimal to a Short. + * If the BigDecimal is too big to fit in a Byte, only the low-order 16 bits are returned. * Note that this conversion can lose information about the overall magnitude of the * BigDecimal value as well as return a result with the opposite sign. */ override def shortValue = intValue.toShort - /** Converts this BigDecimal to a char. + /** Converts this BigDecimal to a Char. * If the BigDecimal is too big to fit in a char, only the low-order 16 bits are returned. * Note that this conversion can lose information about the overall magnitude of the * BigDecimal value and that it always returns a positive result. */ def charValue = intValue.toChar - /** Converts this BigDecimal to an int. + /** Converts this BigDecimal to an Int. * If the BigDecimal is too big to fit in a char, only the low-order 32 bits * are returned. Note that this conversion can lose information about the * overall magnitude of the BigDecimal value as well as return a result with @@ -333,7 +335,7 @@ extends ScalaNumber with ScalaNumericConversions with Serializable */ def intValue = this.bigDecimal.intValue - /** Converts this BigDecimal to a Long. + /** Converts this BigDecimal to a Long. * If the BigDecimal is too big to fit in a char, only the low-order 64 bits * are returned. Note that this conversion can lose information about the * overall magnitude of the BigDecimal value as well as return a result with @@ -341,17 +343,17 @@ extends ScalaNumber with ScalaNumericConversions with Serializable */ def longValue = this.bigDecimal.longValue - /** Converts this BigDecimal to a float. + /** Converts this BigDecimal to a Float. * if this BigDecimal has too great a magnitude to represent as a float, - * it will be converted to Float.NEGATIVE_INFINITY or - * Float.POSITIVE_INFINITY as appropriate. + * it will be converted to `Float.NEGATIVE_INFINITY` or + * `Float.POSITIVE_INFINITY` as appropriate. */ def floatValue = this.bigDecimal.floatValue - /** Converts this BigDecimal to a Double. + /** Converts this BigDecimal to a Double. * if this BigDecimal has too great a magnitude to represent as a double, - * it will be converted to Double.NEGATIVE_INFINITY or - * Double.POSITIVE_INFINITY as appropriate. + * it will be converted to `Double.NEGATIVE_INFINITY` or + * `Double.POSITIVE_INFINITY` as appropriate. */ def doubleValue = this.bigDecimal.doubleValue @@ -363,14 +365,14 @@ extends ScalaNumber with ScalaNumericConversions with Serializable def toLongExact = bigDecimal.longValueExact /** Creates a partially constructed NumericRange[BigDecimal] in range - * [start;end), where start is the target BigDecimal. The step + * `[start;end)`, where start is the target BigDecimal. The step * must be supplied via the "by" method of the returned object in order * to receive the fully constructed range. For example: - *
+   * {{{
    * val partial = BigDecimal(1.0) to 2.0       // not usable yet
    * val range = partial by 0.01                // now a NumericRange
    * val range2 = BigDecimal(0) to 1.0 by 0.01  // all at once of course is fine too
-   * 
+ * }}} * * @param end the end value of the range (exclusive) * @return the partially constructed NumericRange @@ -378,21 +380,21 @@ extends ScalaNumber with ScalaNumericConversions with Serializable def until(end: BigDecimal): Range.Partial[BigDecimal, NumericRange.Exclusive[BigDecimal]] = new Range.Partial(until(end, _)) - /** Same as the one-argument until, but creates the range immediately. */ + /** Same as the one-argument `until`, but creates the range immediately. */ def until(end: BigDecimal, step: BigDecimal) = Range.BigDecimal(this, end, step) - /** Like until, but inclusive of the end value. */ + /** Like `until`, but inclusive of the end value. */ def to(end: BigDecimal): Range.Partial[BigDecimal, NumericRange.Inclusive[BigDecimal]] = new Range.Partial(to(end, _)) - /** Like until, but inclusive of the end value. */ + /** Like `until`, but inclusive of the end value. */ def to(end: BigDecimal, step: BigDecimal) = Range.BigDecimal.inclusive(this, end, step) - /** Converts this BigDecimal to a scala.BigInt. + /** Converts this `BigDecimal` to a scala.BigInt. */ def toBigInt(): BigInt = new BigInt(this.bigDecimal.toBigInteger()) - /** Converts this BigDecimal to a scala.BigInt if it + /** Converts this `BigDecimal` to a scala.BigInt if it * can be done losslessly, returning Some(BigInt) or None. */ def toBigIntExact(): Option[BigInt] = -- cgit v1.2.3