Package totalcross.util
Class BigDecimal
- java.lang.Object
-
- totalcross.util.BigDecimal
-
- All Implemented Interfaces:
Comparable
public class BigDecimal extends java.lang.Object implements Comparable
Arbitrary-precision signed decimal numbers. ABigDecimalconsists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by theBigDecimalis therefore (unscaledValue × 10-scale).
-
-
Field Summary
Fields Modifier and Type Field Description static BigDecimalONEThe constant one as aBigDecimalwith scale zero.static intROUND_CEILINGRounding mode to round towards positive infinity.static intROUND_DOWNRounding mode to round towards zero.static intROUND_FLOORRounding mode to round towards negative infinity.static intROUND_HALF_DOWNRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round down.static intROUND_HALF_EVENRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round towards the even neighbor.static intROUND_HALF_UPRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round up.static intROUND_UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.static intROUND_UPRounding mode to round away from zero.static BigDecimalTENThe constant ten as aBigDecimalwith scale zero.static BigDecimalZEROThe constant zero as aBigDecimalwith scale zero.
-
Constructor Summary
Constructors Constructor Description BigDecimal(double num)Translates adoubleinto aBigDecimalwhich is the exact decimal representation of thedoubles binary floating-point value.BigDecimal(int val)Constructs a newBigDecimalwhose unscaled value isvaland whose scale is zero.BigDecimal(long val)Constructs a newBigDecimalwhose unscaled value isvaland whose scale is zero.BigDecimal(java.lang.String num)Translates the string representation of aBigDecimalinto aBigDecimalobject.BigDecimal(BigInteger num)Constructs a newBigDecimalwhose unscaled value isnumand whose scale is zero.BigDecimal(BigInteger num, int scale)Constructs a newBigDecimalwhose unscaled value isnumand with the given scale.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigDecimalabs()BigDecimaladd(BigDecimal val)Returns aBigDecimalwhose value is(this + val), and whose scale ismax(this.scale(), val.scale()).bytebyteValueExact()Converts this BigDecimal into a byte by first calling longValueExact and then checking that thelongreturned from that method fits into abyte.intcompareTo(java.lang.Object other)Must return > 0 if this object is greater than the other one, < 0 if its smaller, and 0 if they are equal.intcompareTo(BigDecimal val)BigDecimaldivide(BigDecimal divisor)Returns aBigDecimalwhose value is(this / val), and whose scale isthis.scale().BigDecimaldivide(BigDecimal val, int roundingMode)Returns aBigDecimalwhose value is(this / val), and whose scale isthis.scale().BigDecimaldivide(BigDecimal val, int newScale, int roundingMode)Returns aBigDecimalwhose value is(this / val), and whose scale is as specified.BigDecimal[]divideAndRemainder(BigDecimal val)Returns aBigDecimalarray, where its first element is the integer part ofthis / val, and its second element is the remainder of the division.BigDecimaldivideToIntegralValue(BigDecimal val)Returns a BigDecimal whose value is the integer part of the quotient this / val.doubledoubleValue()booleanequals(java.lang.Object o)inthashCode()intintValue()intintValueExact()Converts this BigDecimal into an int by first calling longValueExact and then checking that thelongreturned from that method fits into anint.longlongValue()longlongValueExact()Converts this BigDecimal to a long value.BigDecimalmax(BigDecimal val)BigDecimalmin(BigDecimal val)BigDecimalmovePointLeft(int n)BigDecimalmovePointRight(int n)BigDecimalmultiply(BigDecimal val)Returns aBigDecimalwhose value is (this × val), and whose scale is(this.scale() + val.scale()).BigDecimalnegate()BigDecimalplus()Returns this BigDecimal.BigDecimalpow(int n)Returns a BigDecimal whose value isthisto the power ofn.intprecision()Returns the precision of this BigDecimal (the number of digits in the unscaled value).BigDecimalremainder(BigDecimal val)Returns aBigDecimalwhose value is the remainder in the quotientthis / val.intscale()BigDecimalscaleByPowerOfTen(int n)Returns a BigDecimal whose numerical value is the numerical value of this BigDecimal multiplied by 10 to the power ofn.BigDecimalsetScale(int scale)BigDecimalsetScale(int scale, int roundingMode)shortshortValueExact()Converts this BigDecimal into a short by first calling longValueExact and then checking that thelongreturned from that method fits into ashort.intsignum()BigDecimalstripTrailingZeros()Returns a BigDecimal which is numerically equal to this BigDecimal but with no trailing zeros in the representation.BigDecimalsubtract(BigDecimal val)Returns aBigDecimalwhose value is(this - val), and whose scale ismax(this.scale(), val.scale()).BigIntegertoBigInteger()Converts this BigDecimal to a BigInteger.BigIntegertoBigIntegerExact()Converts this BigDecimal into a BigInteger, throwing an ArithmeticException if the conversion is not exact.java.lang.StringtoEngineeringString()Returns the String representation of this BigDecimal, using engineering notation if necessary.java.lang.StringtoPlainString()Returns a String representation of this BigDecimal without using scientific notation.java.lang.StringtoString()Returns the String representation of this BigDecimal, using scientific notation if necessary.BigDecimalulp()Returns the size of a unit in the last place of this BigDecimal.BigIntegerunscaledValue()static BigDecimalvalueOf(double val)Returns a new BigDecimal constructed from the BigDecimal(String) constructor using the Double .toString(double) method to obtain the String.static BigDecimalvalueOf(long val)Translates alongvalue into aBigDecimalobject with a scale of zero.static BigDecimalvalueOf(long val, int scale)Translates alongunscaled value and anintscale into aBigDecimal.
-
-
-
Field Detail
-
ZERO
public static final BigDecimal ZERO
The constant zero as aBigDecimalwith scale zero.
-
ONE
public static final BigDecimal ONE
The constant one as aBigDecimalwith scale zero.
-
TEN
public static final BigDecimal TEN
The constant ten as aBigDecimalwith scale zero.
-
ROUND_UP
public static final int ROUND_UP
Rounding mode to round away from zero.- See Also:
- Constant Field Values
-
ROUND_DOWN
public static final int ROUND_DOWN
Rounding mode to round towards zero.- See Also:
- Constant Field Values
-
ROUND_CEILING
public static final int ROUND_CEILING
Rounding mode to round towards positive infinity.- See Also:
- Constant Field Values
-
ROUND_FLOOR
public static final int ROUND_FLOOR
Rounding mode to round towards negative infinity.- See Also:
- Constant Field Values
-
ROUND_HALF_UP
public static final int ROUND_HALF_UP
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round up.- See Also:
- Constant Field Values
-
ROUND_HALF_DOWN
public static final int ROUND_HALF_DOWN
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round down.- See Also:
- Constant Field Values
-
ROUND_HALF_EVEN
public static final int ROUND_HALF_EVEN
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round towards the even neighbor.- See Also:
- Constant Field Values
-
ROUND_UNNECESSARY
public static final int ROUND_UNNECESSARY
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BigDecimal
public BigDecimal(java.lang.String num) throws InvalidNumberExceptionTranslates the string representation of aBigDecimalinto aBigDecimalobject.- Parameters:
num- TheBigDecimalas a string.- Throws:
InvalidNumberException- If the given number is not a valid representation of aBigDecimal.
-
BigDecimal
public BigDecimal(int val)
Constructs a newBigDecimalwhose unscaled value isvaland whose scale is zero.- Parameters:
val- The value of the newBigDecimal.
-
BigDecimal
public BigDecimal(long val)
Constructs a newBigDecimalwhose unscaled value isvaland whose scale is zero.- Parameters:
val- The value of the newBigDecimal.
-
BigDecimal
public BigDecimal(BigInteger num)
Constructs a newBigDecimalwhose unscaled value isnumand whose scale is zero.- Parameters:
num- The value of the newBigDecimal.
-
BigDecimal
public BigDecimal(BigInteger num, int scale)
Constructs a newBigDecimalwhose unscaled value isnumand with the given scale.- Parameters:
num- The unscaled value of the newBigDecimal.scale- The given scale.
-
BigDecimal
public BigDecimal(double num) throws InvalidNumberExceptionTranslates adoubleinto aBigDecimalwhich is the exact decimal representation of thedoubles binary floating-point value. The scale of the returnedBigDecimalis the smallest value such that (10scale × val) is an integer.- Parameters:
num- The number to be converted- Throws:
InvalidNumberException- If the number passed is infinite or NaN.
-
-
Method Detail
-
valueOf
public static BigDecimal valueOf(long val) throws InvalidNumberException
Translates alongvalue into aBigDecimalobject with a scale of zero. This "static factory method" is provided in preference to a(long)constructor because it allows for reuse of frequently usedBigDecimalvalues.- Parameters:
val- The value of theBigDecimal.- Returns:
- A
BigDecimalwhose value isval. - Throws:
InvalidNumberException
-
valueOf
public static BigDecimal valueOf(long val, int scale) throws InvalidNumberException
Translates alongunscaled value and anintscale into aBigDecimal. This "static factory method" is provided in preference to a(long, int)constructor because it allows for reuse of frequently usedBigDecimalvalues.- Parameters:
scale- Scale of theBigDecimal.- Returns:
- A
BigDecimalwhose value is (unscaledVal × 10-scale). - Throws:
InvalidNumberException
-
add
public BigDecimal add(BigDecimal val)
Returns aBigDecimalwhose value is(this + val), and whose scale ismax(this.scale(), val.scale()).- Parameters:
val- The value to be added to thisBigDecimal.- Returns:
(this + val).
-
subtract
public BigDecimal subtract(BigDecimal val)
Returns aBigDecimalwhose value is(this - val), and whose scale ismax(this.scale(), val.scale()).- Parameters:
val- The value to be subtracted from thisBigDecimal.- Returns:
(this - val).
-
multiply
public BigDecimal multiply(BigDecimal val)
Returns aBigDecimalwhose value is (this × val), and whose scale is(this.scale() + val.scale()).- Parameters:
val- The value to be multiplied by thisBigDecimal.- Returns:
(this * val).
-
divide
public BigDecimal divide(BigDecimal val, int roundingMode) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException
Returns aBigDecimalwhose value is(this / val), and whose scale isthis.scale(). If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied.- Parameters:
val- The value by which thisBigDecimalis to be divided.roundingMode- The rounding mode to apply.- Returns:
(this / val).- Throws:
java.lang.ArithmeticException- Ifval == 0, orroundingMode == ROUND_UNNECESSARYandthis.scale()is insufficient to represent the result of the division exactly.java.lang.IllegalArgumentException- IfroundingModedoes not represent a valid rounding mode.
-
divide
public BigDecimal divide(BigDecimal val, int newScale, int roundingMode) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException
Returns aBigDecimalwhose value is(this / val), and whose scale is as specified. If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied.- Parameters:
val- The value by which thisBigDecimalis to be divided.roundingMode- The rounding mode to apply.- Returns:
(this / val).- Throws:
java.lang.ArithmeticException- Ifval == 0, orroundingMode == ROUND_UNNECESSARYandthis.scale()is insufficient to represent the result of the division exactly.java.lang.IllegalArgumentException- IfroundingModedoes not represent a valid rounding mode.
-
divide
public BigDecimal divide(BigDecimal divisor) throws java.lang.ArithmeticException
Returns aBigDecimalwhose value is(this / val), and whose scale isthis.scale(). If rounding must be performed to generate a result with the given scale, anArithmeticExceptionis thrown.- Parameters:
divisor- The value by which thisBigDecimalis to be divided.- Returns:
(this / val).- Throws:
java.lang.ArithmeticException- Ifval == 0, orthis.scale()is insufficient to represent the result of the division exactly.
-
remainder
public BigDecimal remainder(BigDecimal val) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException, InvalidNumberException
Returns aBigDecimalwhose value is the remainder in the quotientthis / val. This is obtained bysubtract(divideToIntegralValue(val).multiply(val)).- Parameters:
val- The divisor.- Returns:
- A
BigDecimalwhose value is the remainder - Throws:
InvalidNumberException- If an internal method throws it.java.lang.IllegalArgumentException- If an internal method throws it.java.lang.ArithmeticException- Ifval == 0.
-
divideAndRemainder
public BigDecimal[] divideAndRemainder(BigDecimal val) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException, InvalidNumberException
Returns aBigDecimalarray, where its first element is the integer part ofthis / val, and its second element is the remainder of the division.- Parameters:
val- The divisor.- Returns:
- The above described
BigDecimalarray. - Throws:
InvalidNumberException- If an internal method throws it.java.lang.IllegalArgumentException- If an internal method throws it.java.lang.ArithmeticException- Ifval == 0.
-
divideToIntegralValue
public BigDecimal divideToIntegralValue(BigDecimal val) throws java.lang.ArithmeticException, java.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:
InvalidNumberExceptionjava.lang.IllegalArgumentExceptionjava.lang.ArithmeticExceptionjava.lang.ArithmeticException- if val == 0
-
compareTo
public int compareTo(BigDecimal val)
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.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 java.lang.String toString()
Returns the 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 ifscale == 0is returned. 2. anint adjExpis created which is equal to the negation ofscaleplus the number of digits in the unscaled value, minus one. 3. ifscale >= 0 && adjExp >= -6then 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.- Overrides:
toStringin classjava.lang.Object
-
toEngineeringString
public java.lang.String toEngineeringString()
Returns the 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 String representation of this BigDecimal in engineering notation
-
toPlainString
public java.lang.String toPlainString()
Returns a 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 String 1234000, but the toString() method would return 1.234E+6.- Returns:
- a 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 java.lang.ArithmeticException
- Throws:
java.lang.ArithmeticException
-
setScale
public BigDecimal setScale(int scale, int roundingMode) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException
- Throws:
java.lang.ArithmeticExceptionjava.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 ofn.- Parameters:
n- the power of ten- Returns:
- the new BigDecimal
-
pow
public BigDecimal pow(int n)
Returns a BigDecimal whose value isthisto the power ofn.- 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:
java.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 thelongreturned from that method fits into anint.- Returns:
- an int whose value is
this - Throws:
java.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 thelongreturned from that method fits into abyte.- Returns:
- a byte whose value is
this - Throws:
java.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 thelongreturned from that method fits into ashort.- Returns:
- a short whose value is
this - Throws:
java.lang.ArithmeticException- if this BigDecimal has a fractional part or is too large to fit into a short.
-
compareTo
public int compareTo(java.lang.Object other) throws java.lang.ClassCastExceptionDescription copied from interface:ComparableMust return > 0 if this object is greater than the other one, < 0 if its smaller, and 0 if they are equal.- Specified by:
compareToin interfaceComparable- Throws:
java.lang.ClassCastException
-
-