totalcross.util
Class BigDecimal

totalcross.lang.Object
  extended by totalcross.util.BigDecimal
All Implemented Interfaces:
Comparable

public class BigDecimal
extends totalcross.lang.Object
implements Comparable


Field Summary
static BigDecimal ONE
          The constant one as a BigDecimal with scale zero.
static int ROUND_CEILING
          RoundingMode
static int ROUND_DOWN
          RoundingMode
static int ROUND_FLOOR
          RoundingMode
static int ROUND_HALF_DOWN
          RoundingMode
static int ROUND_HALF_EVEN
          RoundingMode
static int ROUND_HALF_UP
          RoundingMode
static int ROUND_UNNECESSARY
          RoundingMode
static int ROUND_UP
          RoundingMode
static BigDecimal TEN
          The constant ten as a BigDecimal with scale zero.
static BigDecimal ZERO
          The constant zero as a BigDecimal with scale zero.
 
Constructor Summary
BigDecimal(BigInteger num)
          Constructs a BigDecimal whose unscaled value is num and whose scale is zero.
BigDecimal(BigInteger num, int scale)
          Constructs a BigDecimal whose unscaled value is num and whose scale is scale.
BigDecimal(double num)
           
BigDecimal(int val)
          Constructs a new BigDecimal whose unscaled value is val and whose scale is zero.
BigDecimal(long val)
          Constructs a new BigDecimal whose unscaled value is val and whose scale is zero.
BigDecimal(String num)
           
 
Method Summary
 BigDecimal abs()
           
 BigDecimal add(BigDecimal val)
           
 byte byteValueExact()
          Converts this BigDecimal into a byte by first calling longValueExact and then checking that the long returned from that method fits into a byte.
 int compareTo(BigDecimal val)
           
 int compareTo(Object other)
          Must return > 0 if this object is greater than the other one, < 0 if its smaller, and 0 if they are equal.
 BigDecimal divide(BigDecimal divisor)
          Performs division, if the resulting quotient requires rounding (has a nonterminating decimal expansion), an ArithmeticException is thrown.
 BigDecimal divide(BigDecimal val, int roundingMode)
           
 BigDecimal divide(BigDecimal val, int newScale, int roundingMode)
           
 BigDecimal[] divideAndRemainder(BigDecimal val)
          Returns a BigDecimal array, the first element of which is the integer part of this / val, and the second element of which is the remainder of that quotient.
 BigDecimal divideToIntegralValue(BigDecimal val)
          Returns a BigDecimal whose value is the integer part of the quotient this / val.
 double doubleValue()
           
 boolean equals(Object o)
           
 int hashCode()
           
 int intValue()
           
 int intValueExact()
          Converts this BigDecimal into an int by first calling longValueExact and then checking that the long returned from that method fits into an int.
 long longValue()
           
 long longValueExact()
          Converts this BigDecimal to a long value.
 BigDecimal max(BigDecimal val)
           
 BigDecimal min(BigDecimal val)
           
 BigDecimal movePointLeft(int n)
           
 BigDecimal movePointRight(int n)
           
 BigDecimal multiply(BigDecimal val)
           
 BigDecimal negate()
           
 BigDecimal plus()
          Returns this BigDecimal.
 BigDecimal pow(int n)
          Returns a BigDecimal whose value is this to the power of n.
 int precision()
          Returns the precision of this BigDecimal (the number of digits in the unscaled value).
 BigDecimal remainder(BigDecimal val)
          Returns a BigDecimal whose value is the remainder in the quotient this / val.
 int scale()
           
 BigDecimal scaleByPowerOfTen(int n)
          Returns a BigDecimal whose numerical value is the numerical value of this BigDecimal multiplied by 10 to the power of n.
 BigDecimal setScale(int scale)
           
 BigDecimal setScale(int scale, int roundingMode)
           
 short shortValueExact()
          Converts this BigDecimal into a short by first calling longValueExact and then checking that the long returned from that method fits into a short.
 int signum()
           
 BigDecimal stripTrailingZeros()
          Returns a BigDecimal which is numerically equal to this BigDecimal but with no trailing zeros in the representation.
 BigDecimal subtract(BigDecimal val)
           
 BigInteger toBigInteger()
          Converts this BigDecimal to a BigInteger.
 BigInteger toBigIntegerExact()
          Converts this BigDecimal into a BigInteger, throwing an ArithmeticException if the conversion is not exact.
 String toEngineeringString()
          Returns the totalcross.lang.String representation of this BigDecimal, using engineering notation if necessary.
 String toPlainString()
          Returns a totalcross.lang.String representation of this BigDecimal without using scientific notation.
 String toString()
          Returns the totalcross.lang.String representation of this BigDecimal, using scientific notation if necessary.
 BigDecimal ulp()
          Returns the size of a unit in the last place of this BigDecimal.
 BigInteger unscaledValue()
           
static BigDecimal valueOf(double val)
          Returns a new BigDecimal constructed from the BigDecimal(String) constructor using the Double .toString(double) method to obtain the String.
static BigDecimal valueOf(long val)
           
static BigDecimal valueOf(long val, int scale)
           
 
Methods inherited from class totalcross.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final BigDecimal ZERO
The constant zero as a BigDecimal with scale zero.


ONE

public static final BigDecimal ONE
The constant one as a BigDecimal with scale zero.


TEN

public static final BigDecimal TEN
The constant ten as a BigDecimal with scale zero.


ROUND_UP

public static final int ROUND_UP
RoundingMode

See Also:
Constant Field Values

ROUND_DOWN

public static final int ROUND_DOWN
RoundingMode

See Also:
Constant Field Values

ROUND_CEILING

public static final int ROUND_CEILING
RoundingMode

See Also:
Constant Field Values

ROUND_FLOOR

public static final int ROUND_FLOOR
RoundingMode

See Also:
Constant Field Values

ROUND_HALF_UP

public static final int ROUND_HALF_UP
RoundingMode

See Also:
Constant Field Values

ROUND_HALF_DOWN

public static final int ROUND_HALF_DOWN
RoundingMode

See Also:
Constant Field Values

ROUND_HALF_EVEN

public static final int ROUND_HALF_EVEN
RoundingMode

See Also:
Constant Field Values

ROUND_UNNECESSARY

public static final int ROUND_UNNECESSARY
RoundingMode

See Also:
Constant Field Values
Constructor Detail

BigDecimal

public BigDecimal(String num)
           throws InvalidNumberException
Throws:
InvalidNumberException

BigDecimal

public BigDecimal(int val)
Constructs a new BigDecimal whose unscaled value is val and whose scale is zero.

Parameters:
val - the value of the new BigDecimal

BigDecimal

public BigDecimal(long val)
Constructs a new BigDecimal whose unscaled value is val and whose scale is zero.

Parameters:
val - the value of the new BigDecimal

BigDecimal

public BigDecimal(BigInteger num)
Constructs a BigDecimal whose unscaled value is num and whose scale is zero.

Parameters:
num - the value of the new BigDecimal

BigDecimal

public BigDecimal(BigInteger num,
                  int scale)
Constructs a BigDecimal whose unscaled value is num and whose scale is scale.

Parameters:
num -
scale -

BigDecimal

public BigDecimal(double num)
           throws InvalidNumberException
Throws:
InvalidNumberException
Method Detail

valueOf

public static BigDecimal valueOf(long val)
                          throws InvalidNumberException
Throws:
InvalidNumberException

valueOf

public static BigDecimal valueOf(long val,
                                 int scale)
                          throws InvalidNumberException
Throws:
InvalidNumberException

add

public BigDecimal add(BigDecimal val)

subtract

public BigDecimal subtract(BigDecimal val)

multiply

public BigDecimal multiply(BigDecimal val)

divide

public BigDecimal divide(BigDecimal val,
                         int roundingMode)
                  throws totalcross.lang.ArithmeticException,
                         totalcross.lang.IllegalArgumentException
Throws:
totalcross.lang.ArithmeticException
totalcross.lang.IllegalArgumentException

divide

public BigDecimal divide(BigDecimal val,
                         int newScale,
                         int roundingMode)
                  throws totalcross.lang.ArithmeticException,
                         totalcross.lang.IllegalArgumentException
Throws:
totalcross.lang.ArithmeticException
totalcross.lang.IllegalArgumentException

divide

public BigDecimal divide(BigDecimal divisor)
                  throws totalcross.lang.ArithmeticException,
                         totalcross.lang.IllegalArgumentException
Performs division, if the resulting quotient requires rounding (has a nonterminating decimal expansion), an ArithmeticException is thrown. #see divide(BigDecimal, int, int)

Throws:
totalcross.lang.ArithmeticException
totalcross.lang.IllegalArgumentException

remainder

public BigDecimal remainder(BigDecimal val)
                     throws totalcross.lang.ArithmeticException,
                            totalcross.lang.IllegalArgumentException,
                            InvalidNumberException
Returns a BigDecimal whose value is the remainder in the quotient this / val. This is obtained by subtract(divideToIntegralValue(val).multiply(val)).

Parameters:
val - the divisor
Returns:
a BigDecimal whose value is the remainder
Throws:
InvalidNumberException
totalcross.lang.IllegalArgumentException
totalcross.lang.ArithmeticException
totalcross.lang.ArithmeticException - if val == 0

divideAndRemainder

public BigDecimal[] divideAndRemainder(BigDecimal val)
                                throws totalcross.lang.ArithmeticException,
                                       totalcross.lang.IllegalArgumentException,
                                       InvalidNumberException
Returns a BigDecimal array, the first element of which is the integer part of this / val, and the second element of which is the remainder of that quotient.

Parameters:
val - the divisor
Returns:
the above described BigDecimal array
Throws:
InvalidNumberException
totalcross.lang.IllegalArgumentException
totalcross.lang.ArithmeticException
totalcross.lang.ArithmeticException - if val == 0

divideToIntegralValue

public BigDecimal divideToIntegralValue(BigDecimal val)
                                 throws totalcross.lang.ArithmeticException,
                                        totalcross.lang.IllegalArgumentException,
                                        InvalidNumberException
Returns a BigDecimal whose value is the integer part of the quotient this / val. The preferred scale is this.scale - val.scale.

Parameters:
val - the divisor
Returns:
a BigDecimal whose value is the integer part of this / val.
Throws:
InvalidNumberException
totalcross.lang.IllegalArgumentException
totalcross.lang.ArithmeticException
totalcross.lang.ArithmeticException - if val == 0

compareTo

public int compareTo(BigDecimal val)

equals

public boolean equals(Object o)

hashCode

public int hashCode()
Overrides:
hashCode in class totalcross.lang.Object

max

public BigDecimal max(BigDecimal val)

min

public BigDecimal min(BigDecimal val)

movePointLeft

public BigDecimal movePointLeft(int n)

movePointRight

public BigDecimal movePointRight(int n)

signum

public int signum()

scale

public int scale()

unscaledValue

public BigInteger unscaledValue()

abs

public BigDecimal abs()

negate

public BigDecimal negate()

plus

public BigDecimal plus()
Returns this BigDecimal. This is included for symmetry with the method negate().

Returns:
this

precision

public int precision()
Returns the precision of this BigDecimal (the number of digits in the unscaled value). The precision of a zero value is 1.

Returns:
the number of digits in the unscaled value, or 1 if the value is zero.

toString

public String toString()
Returns the totalcross.lang.String representation of this BigDecimal, using scientific notation if necessary. The following steps are taken to generate the result: 1. the BigInteger unscaledValue's toString method is called and if scale == 0 is returned. 2. an int adjExp is created which is equal to the negation of scale plus the number of digits in the unscaled value, minus one. 3. if scale >= 0 && adjExp >= -6 then we represent this BigDecimal without scientific notation. A decimal is added if the scale is positive and zeros are prepended as necessary. 4. if scale is negative or adjExp is less than -6 we use scientific notation. If the unscaled value has more than one digit, a decimal as inserted after the first digit, the character 'E' is appended and adjExp is appended.


toEngineeringString

public String toEngineeringString()
Returns the totalcross.lang.String representation of this BigDecimal, using engineering notation if necessary. This is similar to toString() but when exponents are used the exponent is made to be a multiple of 3 such that the integer part is between 1 and 999.

Returns:
a totalcross.lang.String representation of this BigDecimal in engineering notation

toPlainString

public String toPlainString()
Returns a totalcross.lang.String representation of this BigDecimal without using scientific notation. This is how toString() worked for releases 1.4 and previous. Zeros may be added to the end of the String. For example, an unscaled value of 1234 and a scale of -3 would result in the totalcross.lang.String 1234000, but the toString() method would return 1.234E+6.

Returns:
a totalcross.lang.String representation of this BigDecimal

toBigInteger

public BigInteger toBigInteger()
Converts this BigDecimal to a BigInteger. Any fractional part will be discarded.

Returns:
a BigDecimal whose value is equal to floor[this]

toBigIntegerExact

public BigInteger toBigIntegerExact()
Converts this BigDecimal into a BigInteger, throwing an ArithmeticException if the conversion is not exact.

Returns:
a BigInteger whose value is equal to the value of this BigDecimal

intValue

public int intValue()

stripTrailingZeros

public BigDecimal stripTrailingZeros()
                              throws InvalidNumberException
Returns a BigDecimal which is numerically equal to this BigDecimal but with no trailing zeros in the representation. For example, if this BigDecimal has [unscaledValue, scale] = [6313000, 4] this method returns a BigDecimal with [unscaledValue, scale] = [6313, 1]. As another example, [12400, -2] would become [124, -4].

Returns:
a numerically equal BigDecimal with no trailing zeros
Throws:
InvalidNumberException

longValue

public long longValue()

doubleValue

public double doubleValue()
                   throws InvalidNumberException
Throws:
InvalidNumberException

setScale

public BigDecimal setScale(int scale)
                    throws totalcross.lang.ArithmeticException
Throws:
totalcross.lang.ArithmeticException

setScale

public BigDecimal setScale(int scale,
                           int roundingMode)
                    throws totalcross.lang.ArithmeticException,
                           totalcross.lang.IllegalArgumentException
Throws:
totalcross.lang.ArithmeticException
totalcross.lang.IllegalArgumentException

valueOf

public static BigDecimal valueOf(double val)
                          throws InvalidNumberException
Returns a new BigDecimal constructed from the BigDecimal(String) constructor using the Double .toString(double) method to obtain the String.

Parameters:
val - the double value used in Double .toString(double)
Returns:
a BigDecimal representation of val
Throws:
InvalidNumberException - if val is NaN or infinite

scaleByPowerOfTen

public BigDecimal scaleByPowerOfTen(int n)
Returns a BigDecimal whose numerical value is the numerical value of this BigDecimal multiplied by 10 to the power of n.

Parameters:
n - the power of ten
Returns:
the new BigDecimal

pow

public BigDecimal pow(int n)
Returns a BigDecimal whose value is this to the power of n.

Parameters:
n - the power
Returns:
the new BigDecimal

ulp

public BigDecimal ulp()
Returns the size of a unit in the last place of this BigDecimal. This returns a BigDecimal with [unscaledValue, scale] = [1, this.scale()].

Returns:
the size of a unit in the last place of this.

longValueExact

public long longValueExact()
Converts this BigDecimal to a long value.

Returns:
the long value
Throws:
totalcross.lang.ArithmeticException - if rounding occurs or if overflow occurs

intValueExact

public int intValueExact()
Converts this BigDecimal into an int by first calling longValueExact and then checking that the long returned from that method fits into an int.

Returns:
an int whose value is this
Throws:
totalcross.lang.ArithmeticException - if this BigDecimal has a fractional part or is too large to fit into an int.

byteValueExact

public byte byteValueExact()
Converts this BigDecimal into a byte by first calling longValueExact and then checking that the long returned from that method fits into a byte.

Returns:
a byte whose value is this
Throws:
totalcross.lang.ArithmeticException - if this BigDecimal has a fractional part or is too large to fit into a byte.

shortValueExact

public short shortValueExact()
Converts this BigDecimal into a short by first calling longValueExact and then checking that the long returned from that method fits into a short.

Returns:
a short whose value is this
Throws:
totalcross.lang.ArithmeticException - if this BigDecimal has a fractional part or is too large to fit into a short.

compareTo

public int compareTo(Object other)
              throws totalcross.lang.ClassCastException
Description copied from interface: Comparable
Must return > 0 if this object is greater than the other one, < 0 if its smaller, and 0 if they are equal.

Specified by:
compareTo in interface Comparable
Throws:
totalcross.lang.ClassCastException