|
ArciMath BigDecimal v2.05 now with BigDecimalFormat |
|||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Object
|
+--java.lang.Number
|
+--be.arci.math.BigDecimal
The ArciMath BigDecimal class contains immutable arbitrary-precision signed decimal numbers supporting operations for both fixed and floating point arithmetic, comparison, scale manipulation, comparison, hashing, and format conversion. Different forms of notation are provided, to know plain notation (without exponent), and scientific (where one digit is shown before the decimal point) and engineering (where the power of ten is a multiple of three) exponential notations.
The floating point arithmetic provided by this class is defined by the ANSI X3.274-1996 standard (American National Standard for Information Technology – Programming Language REXX, X3.274-1996, American National Standards Institute, New York, 1996.), and is also documented by IBM in a document called decimal.pdf that can be found at http://www2.hursley.ibm.com/decimal (This URL may change!). In this implementation, the maximum precision of a number is 'limited' to 999.999.999 (10E+9 - 1) digits (this gigantic range is the reason why we have not included MAX_VALUE and MIN_VALUE fields), and the exponent of a number must fall within the range -1.000.000.000 (-10E+9) < exponent < +1.000.000.000 (+10E+9). Out-of-range results are detected and throw exceptions.
Integers and fixed-scale numbers are a proper subset of all numbers. Conversions to and from a different class are not necessary in order to carry out integer and currency calculations.
The ArciMath BigDecimal class gives it's user complete control over rounding behavior, forcing the user to explicitly specify a rounding mode for operations capable of discarding precision (such as divide and pow), and allowing to specify a rounding mode elsewhere. Eight rounding modes are provided for this purpose.
Two types of operations are provided for manipulating the scale of an ArciMath
BigDecimal: scaling/rounding operations and decimal point motion operations.
Scaling/rounding operations (SetScale and plus) return an
ArciMath BigDecimal whose value is approximately (or exactly) equal to that
of the ArciMath BigDecimal operated on operand, but whose scale is the specified value;
that is, they increase or decrease the precision of the number with minimal
effect on its value.
Decimal point motion operations (movePointLeft and
movePointRight) return an ArciMath BigDecimal created from the operand by
moving the decimal point a specified distance in the specified direction;
that is, they change a number's value without affecting its precision. As an
example you might express a ArciMath BigDecimal number bd in millions with the operation
bd.movePointLeft(6).
As the numbers are decimal, there is an exact correspondence between an instance of a BigDecimal object and its String representation in decimal digits; the BigDecimal class provides direct conversions to and from String and character array (char[]) objects and from ASCII byte arrays (byte[]), as well as conversions to and from the Java primitive types, java.math.BigInteger, and java.math.BigDecimal. As conversion to integer types is inherently inexact, ArciMath BigDecimal has exact variants of the conversion function, that throw an exception if either the ArciMath BigDecimal number has a non-zero decimal scale, or is too large (in absolute value) to express in the desired integer type (the latter case of course applying to primitive integer types only, not to conversion to BigInteger). Exactness of conversion from the decimal floating point type ArciMath BigDecimal to the binary floating point types double and float can however not be enforced by any means, as most decimal numbers cannot be expressed as binary numbers.
For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of ArciMath BigDecimal methods. The pseudo-code expression (f + g) is shorthand for "an ArciMath BigDecimal whose value is that of the ArciMath BigDecimal f plus that of the ArciMath BigDecimal g." The pseudo-code expression (f == g) is shorthand for "true if and only if the ArciMath BigDecimal f represents the same value as the the ArciMath BigDecimal g." Other pseudo-code expressions are interpreted similarly. In the descriptions of constructors and methods in this documentation, the value of a BigDecimal number object is shown as the result of invoking the toString() method on that object.
The internal representation of a decimal number is neither defined nor exposed, and is not permitted to affect the result of any operation. For the interested however we can give away that ArciMath BigDecimal internally uses a modified BCD (Binary Coded Decimal) scheme, that codes 2 decimal digits per byte, with 4 BCD limbs packed in an int[] for moving around fast. The evaluation version uses one BCD limb less per int, so as to offer exactly the same functionality, value range and precision as the production version, but at a higher memory consumption and a reduced speed (depending on the operator method and the length of the number, the speed difference can vary from 10% to 70%).
It may also interest you that, as this class is meant to be a plugin replacement for java.math.BigDecimal, in no place the code of this class delegates to methods of either java.math.BigDecimal or java.math.BigInteger, except in the constructors from either one of the java.math classes and the methods converting to them.
For monadic operators, only the optional MathContext parameter is present; the operation acts upon the current ArciMath BigDecimal object.
For dyadic operators, a BigDecimal parameter is always present, which must not be null. The operation acts with the current ArciMath BigDecimal object as left-hand operand and the BigDecimal parameter as right-hand operand.
For example, dividing two BigDecimal objects referred to by the names bdDividend and bdDivisor could be written as any of:
bdDividend.divide(bdDivisor) bdDividend.divide(bdDivisor, MathContext.DEFAULT) bdDividend.divide(bdDivisor, myContext)(where myContext is a MathContext object), which would return a BigDecimal object whose value is the quotient bdDividend / bdDivisor performed within the appropriate context.
When a BigDecimal operator method is used, a set of rules define what the result will be (and, by implication, how the result would be represented as a character string). These rules are defined in IBM's BigDecimal arithmetic documentation (see the URL above), but in summary:
"1.20" + "1" = "2.20" "1.20" - "1" = "0.20" "1.20" * "1" = "1.20" "1.20" / "1" = "1.20" "1.20" / "2" = "0.60" "1.20" / "10" = "0.12" "1.20" / "1" = "1.2" (with MathContext.DEFAULT)If necessary, trailing zeros may be easily removed as shown in the above example using division by 1 and a MathContext with a sufficiently high digits setting.
BigDecimal bd = new BigDecimal("123456789.000"), bd2;
bd2 = bd.divide(BigDecimal.ONE, new MathContext(20));
System.out.println(bd + " / " + BigDecimal.ONE + " = " + bd2);
prints 123456789.000 / 1 = 123456789
"1e+6" * "1e+6" = "1E+12" instead of "1000000000000", and "1" / "3E+10" = "3.33333333E-11" instead of "0.0000000000333333333" (both with MathContext.DEFAULT)The form of the exponential notation (SCIENTIFIC or ENGINEERING) is determined by the form property of the MathContext.
The names of methods in this class follow the conventions established by java.math.BigDecimal, java.math.BigInteger, and java.lang.Number in Java 1.1 and Java 1.2.
Note: care should be exercised if ArciMath BigDecimals are to be used as keys in a SortedMap or elements in a SortedSet, as ArciMath BigDecimal's natural ordering is inconsistent with equals; this behaviour is compatible with java.math.BigDecimal behavoir, whose natural ordering equates BigDecimals with equal values and different precisions (such as "4.0" and "4.00"). See Comparable, SortedMap or SortedSet for more information.
MathContext, Serialized Form| Field Summary | |
static java.lang.String |
copy
Copyright notice for ArciMath BigDecimal; this String also can serve to distinguish the evaluation version and the producion version of ArciMath BigDecimal. |
protected static int |
iRADIX
The radix of our BCD coding after AAD (ASCII adjust for division); this value is 100. |
static BigDecimal |
ONE
The BigDecimal constant "1". |
static int |
ROUND_CEILING
Rounding mode to round towards positive infinity. |
static int |
ROUND_DOWN
Rounding mode to round towards zero. |
static int |
ROUND_FLOOR
Rounding mode to round towards negative infinity. |
static int |
ROUND_HALF_DOWN
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. |
static 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. |
static int |
ROUND_HALF_UP
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. |
static int |
ROUND_UNNECESSARY
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. |
static int |
ROUND_UP
Rounding mode to round away from zero. |
static BigDecimal |
TEN
The BigDecimal constant "10". |
static BigDecimal |
TWO
The BigDecimal constant "2". |
static BigDecimal |
ZERO
The BigDecimal constant "0". |
| Constructor Summary | |
BigDecimal(BigDecimal bd)
Construct a copy of the BigDecimal argument. |
|
BigDecimal(java.math.BigDecimal bd)
Constructs a be.arci.math.BigDecimal object from a java.math.BigDecimal as though the parameter had been represented as a String (using its toString() method) and the be.arci.math.BigDecimal(java.lang.String) constructor had then been used. |
|
BigDecimal(java.math.BigInteger bi)
Constructs a BigDecimal which is the exact decimal representation of the java.math.BigInteger, with a scale of zero. |
|
BigDecimal(java.math.BigInteger bi,
int iScale)
Translates a java.math.BigInteger unscaled value and an int scale into a BigDecimal. |
|
BigDecimal(byte[] abAsciiDigits)
Convenience constructor for locales where the decimal separator is the '.' character. |
|
BigDecimal(byte[] abAsciiDigits,
char cDecimalSeparator)
Constructs a BigDecimal object from an array of bytes. |
|
BigDecimal(byte[] abAsciiDigits,
int iStart,
int iLength)
Convenience constructor for locales where the decimal separator is the '.' character. |
|
BigDecimal(byte[] abAsciiDigits,
int iStart,
int iLength,
char cDecimalSeparator)
Constructs a BigDecimal object from a subarray of bytes. |
|
BigDecimal(char[] ac)
Convenience constructor for locales where the decimal separator is the '.' character. |
|
BigDecimal(char[] ac,
char cDecimalSeparator)
Constructs a BigDecimal object from an array of characters. |
|
BigDecimal(char[] ac,
int iStart,
int iLength)
Convenience constructor for locales where the decimal separator is the '.' character. |
|
BigDecimal(char[] ac,
int iStart,
int iLength,
char cDecimalSeparator)
Constructs a BigDecimal object from a subarray of characters. |
|
BigDecimal(double d)
Deprecated. |
|
BigDecimal(int i)
Fast constructor for wrapping an int value into a BigDecimal object. |
|
BigDecimal(long l)
Fast constructor for wrapping a long value into a BigDecimal object. |
|
BigDecimal(java.lang.String s)
Convenience constructor for locales where the decimal separator is the '.' character. |
|
BigDecimal(java.lang.String s,
char cDecimalSeparator)
Constructs a BigDecimal object from a String, which must represent a valid number, as described formally in IBM's specification referred to above. |
|
| Method Summary | |
BigDecimal |
abs()
Implements the absolute value operator, and returns a positive BigDecimal value equal in magnitude to this value. |
BigDecimal |
abs(MathContext mc)
Implements the absolute value operator, performed within the mathematical context mc, and returns a positive BigDecimal value equal in magnitude to this value (apart from rounding specified by the MathContext). |
BigDecimal |
add(BigDecimal bd)
Implements the addition operator (+), using fixed point arithmetic, and returns a BigDecimal whose scale is max(this.scale(), bd.scale()). |
BigDecimal |
add(BigDecimal bd,
MathContext mc)
Implements the addition operator (+), within the context specified by mc. |
byte |
byteValueExact()
Converts this BigDecimal to a Java byte value (an 8-bit signed integer); if the BigDecimal has a non-zero decimal part or is out of range for a Java byte value then an ArithmeticException is thrown. |
int |
compareTo(BigDecimal bd)
Performs numerical comparison of this BigDecimal with the specified BigDecimal, using unlimited precision. |
int |
compareTo(BigDecimal bd,
MathContext mc)
Performs numerical comparison of this BigDecimal with the specified BigDecimal, within the context specified by mc. |
int |
compareTo(java.lang.Object o)
Performs numerical comparison of this BigDecimal with the specified Object. |
BigDecimal |
divide(BigDecimal bd)
Implements the division (/) operator, using fixed point arithmetic, and returns a BigDecimal quotient whose scale will be the same as the scale of this BigDecimal (the dividend) if this were formatted without exponential notation. |
BigDecimal |
divide(BigDecimal bd,
int roundingMode)
Implements the division (/) operator, using fixed point arithmetic and a rounding mode, and returns a BigDecimal quotient whose scale will be the same as the scale of this BigDecimal (the dividend) if this were formatted without exponential notation. |
BigDecimal |
divide(BigDecimal bd,
int iScale,
int roundingMode)
Implements the division (/) operator, using fixed point arithmetic and a rounding mode, and returns a BigDecimal quotient whose scale is as specified. |
BigDecimal |
divide(BigDecimal bd,
MathContext mc)
Implements the division (/) operator, within the context specified by mc. |
BigDecimal[] |
divideAndRemainder(BigDecimal bd)
Implements the integer division (/) and remainder (%) operators, using fixed point arithmetic. |
BigDecimal[] |
divideAndRemainder(BigDecimal bd,
MathContext mc)
Implements the integer division (/) and remainder (%) operators, within the context specified by mc. |
BigDecimal |
divideInteger(BigDecimal bd)
Implements the integer division (/) operator, using fixed point arithmetic. |
BigDecimal |
divideInteger(BigDecimal bd,
MathContext mc)
Implements the integer division (/) operator, within the context specified by mc. |
double |
doubleValue()
Converts this BigDecimal to a Java double value. |
boolean |
equals(java.lang.Object o)
Compares this BigDecimal with the specified Object for equality. |
float |
floatValue()
Converts this BigDecimal to a Java float value. |
java.lang.String |
format(int iIntDigits,
int iDecDigits)
Modifies the String representation of this BigDecimal according to a set of formatting parameters, without using exponential notation. |
java.lang.String |
format(int iIntDigits,
int iDecDigits,
int iExpDigits,
int iMaxIntDigits,
int iNot,
int roundingMode)
Modifies the String representation of this BigDecimal according to a set of layout parameters, and allowing exponential notation. |
int |
getDigits()
Returns the count of decimal digits in the significand, with a minimum of 1 digit for the BigDecimal ZERO. |
int |
getExponent()
Returns the exponent of this number if it would be expressed in scientific notation (0 <= base number < 1). |
int |
hashCode()
Returns the hashcode for this BigDecimal. |
int |
intValue()
Converts this BigDecimal to a Java int value. |
int |
intValueExact()
Converts this BigDecimal to a Java int value; if the BigDecimal has a non-zero decimal part or is out of the possible range for a Java int (32-bit signed integer) result then an ArithmeticException is thrown. |
boolean |
isByteValue()
Returns true if a call to byteValueExact() will return succesfully (without throwing an ArithmeticException). |
boolean |
isIntValue()
Returns true if a call to intValueExact() will return succesfully (without throwing an ArithmeticException). |
boolean |
isLongValue()
Returns true if a call to longValueExact() will return succesfully (without throwing an ArithmeticException). |
boolean |
isShortValue()
Returns true if a call to shortValueExact() will return succesfully (without throwing an ArithmeticException). |
long |
longValue()
Converts this BigDecimal to a Java long value. |
long |
longValueExact()
Converts this BigDecimal to a Java long value; if the BigDecimal has a non-zero decimal part or is out of the possible range for a Java long (64-bit signed integer) then an ArithmeticException is thrown. |
BigDecimal |
max(BigDecimal bd)
Returns the maximum of this BigDecimal and bd. |
BigDecimal |
max(BigDecimal bd,
MathContext mc)
Returns a BigDecimal whose value is the maximum of this and bd, within the context specified by mc. |
BigDecimal |
min(BigDecimal bd)
Returns the minimum of this BigDecimal and bd. |
BigDecimal |
min(BigDecimal bd,
MathContext mc)
Returns a BigDecimal whose value is the minimum of this and bd, within the context specified by mc. |
BigDecimal |
movePointLeft(int n)
Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left. |
BigDecimal |
movePointRight(int n)
Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right. |
BigDecimal |
multiply(BigDecimal bd)
Implements the multiplication (*) operator, using fixed point arithmetic, and returns a BigDecimal product with scale equal to the sum of the scales of the operands, if they were formatted without exponential notation. |
BigDecimal |
multiply(BigDecimal bd,
MathContext mc)
Implements the multiplication (*) operator, within the context specified by mc. |
BigDecimal |
negate()
Implements the negation operator (unary -), using fixed point arithmetic, and returns a BigDecimal whose value is (-this), and whose scale is this.scale(). |
BigDecimal |
negate(MathContext mc)
Implements the negation operator (unary -), within the context specified by mc. |
BigDecimal |
normalize()
Normalizes this BigDecimal number by removing all trailing zero digits from it's magnitude and adjusting it's exponent accordingly. |
BigDecimal |
plus()
Implements the plus operator (unary +), using fixed point arithmetic, and returns a BigDecimal whose value is (+this), and whose scale is this.scale(). |
BigDecimal |
plus(MathContext mc)
Implements the plus operator (unary +), within the context specified by mc; this method is useful for rounding or otherwise applying a context to a decimal value. |
BigDecimal |
pow(BigDecimal bd)
Implements the exponentiation operator, using fixed point arithmetic. |
BigDecimal |
pow(BigDecimal bd,
MathContext mc)
Implements the exponentiation operator, within the context specified by mc. |
BigDecimal |
pow(int iPower)
Implements the exponentiation operator, using fixed point arithmetic. |
BigDecimal |
pow(int iPower,
MathContext mc)
Implements the exponentiation operator, within the context specified by mc. |
BigDecimal |
remainder(BigDecimal bd)
Implements the remainder (%) operator, using integer division and fixed point arithmetic. |
BigDecimal |
remainder(BigDecimal bd,
MathContext mc)
Implements the remainder (%) operator, using integer division and within the context specified by mc. |
BigDecimal |
round(MathContext mc)
Rounds a BigDecimal value according to the MathContext argument. |
int |
scale()
Returns the scale of this BigDecimal. |
BigDecimal |
setScale(int iScale)
Returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal's. |
BigDecimal |
setScale(int iScale,
int roundingMode)
Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value. |
short |
shortValueExact()
Converts this BigDecimal to a Java short value; if the BigDecimal has a non-zero decimal part or is out of the possible range for a Java short (16-bit signed integer) result then an ArithmeticException is thrown. |
int |
signum()
Returns the sign of this BigDecimal as an int. |
BigDecimal |
subtract(BigDecimal bd)
Implements the subtraction (-) operator, using fixed point arithmetic, and returns a BigDecimal difference whose scale is max(this.scale(), val.scale()). |
BigDecimal |
subtract(BigDecimal bd,
MathContext mc)
Implements the subtraction (-) operator, within the context specified by mc. |
java.math.BigDecimal |
toBigDecimal()
Converts this BigDecimal to a java.math.BigDecimal object. |
java.math.BigInteger |
toBigInteger()
Converts this BigDecimal to a java.math.BigInteger object. |
java.math.BigInteger |
toBigIntegerExact()
Converts this BigDecimal to a java.math.BigInteger object; an exception is thrown if this BigDecimal has a non-zero decimal part. |
char[] |
toCharArray()
Convenience method for locales using the '.' character as decimal separator. |
char[] |
toCharArray(char cDecimalSeparator)
Returns the BigDecimal as a character array. |
java.lang.String |
toString()
Convenience method for locales using the '.' character as decimal separator. |
java.lang.String |
toString(char cDecimalSeparator)
Returns the string representation of this BigDecimal. |
java.math.BigInteger |
unscaledValue()
Returns a java.math.BigInteger whose value is the unscaled value of this BigDecimal; that is, the number is expressed as a plain number, any decimal point is then removed (retaining the digits of any decimal part), and the result is then converted to a java.math.BigInteger. |
static BigDecimal |
valueOf(double d)
Translates a double to a BigDecimal. |
static BigDecimal |
valueOf(long l)
Translates a long value into a BigDecimal with a scale of zero. |
static BigDecimal |
valueOf(long l,
int iScale)
Translates a long unscaled value and an int scale into a BigDecimal. |
| Methods inherited from class java.lang.Number |
byteValue, shortValue |
| Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
protected static final int iRADIX
public static final java.lang.String copy
public static final BigDecimal ZERO
public static final BigDecimal ONE
public static final BigDecimal TWO
Note: This constant is not in com.ibm.math.BigDecimal.
public static final BigDecimal TEN
public static final int ROUND_UP
MathContext.ROUND_UPpublic static final int ROUND_DOWN
MathContext.ROUND_DOWNpublic static final int ROUND_CEILING
MathContext.ROUND_CEILINGpublic static final int ROUND_FLOOR
MathContext.ROUND_FLOORpublic static final int ROUND_HALF_UP
MathContext.ROUND_HALF_UPpublic static final int ROUND_HALF_DOWN
MathContext.ROUND_HALF_DOWNpublic static final int ROUND_HALF_EVEN
MathContext.ROUND_HALF_EVENpublic static final int ROUND_UNNECESSARY
MathContext.ROUND_UNNECESSARY| Constructor Detail |
public BigDecimal(BigDecimal bd)
bd - The number to be copied; must not be nullpublic BigDecimal(int i)
Constructs a BigDecimal which is the exact decimal representation of the 32-bit signed binary integer parameter. The BigDecimal will contain only decimal digits, prefixed with a leading minus sign if the parameter is negative. A leading zero will be present only if the parameter is zero.
i - The int value to be converted.public BigDecimal(long l)
Constructs a BigDecimal which is the exact decimal representation of the 64-bit signed binary integer parameter. The BigDecimal will contain only decimal digits, prefixed with a leading minus sign if the parameter is negative. A leading zero will be present only if the parameter is zero.
l - The long value to be converted.public BigDecimal(double d)
This constructor does not give the same result as converting the double value to a String using the Double.toString() method and then using the BigDecimal(java.lang.String) constructor; because the arithmetic in ArciMath is completely defined with the String representation of numbers, this constructor is deprecated, even though it is more accurate.
Recommendation: Use the static valueOf(double) method to construct a BigDecimal from a double if you expect it to have a String representation with the same value; use this constructor if you want to get the exact value of the binary double, or you need more speed than is possible by going via the String representation of the double.
Note: the results of this constructor can be somewhat unexpected. One might expect that new BigDecimal(0.1) is exactly "0.1", but it is actually "0.1000000000000000055511151231257827021181583404541015625". This is so because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the double value -.1 that seems to be passed in to the constructor is actually the value 0.1000000000000000055511151231257827021181583404541015625.
Stated otherwise, new BigDecimal(0.1) is not the same as new BigDecimal("0.1"). The (String) constructor indeed is perfectly predictable: new BigDecimal("0.1") is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
d - double value to be converted to BigDecimal.java.lang.NumberFormatException - val is equal to
Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY, or Double.NaN.valueOf(double)public BigDecimal(java.math.BigDecimal bd)
bd - The java.math.BigDecimal to be translated;
this parameter must not be null.public BigDecimal(java.math.BigInteger bi)
The BigDecimal will contain only decimal digits, prefixed with a leading minus sign if the BigInteger is negative. A leading zero will be present only if the BigInteger is zero.
bi - BigInteger value to be converted to BigDecimal;
this parameter must not be null.
public BigDecimal(java.math.BigInteger bi,
int iScale)
The BigDecimal will contain only decimal digits, (with an embedded decimal point followed by scale decimal digits if the scale is positive), prefixed with a leading minus sign if the BigInteger is negative. A leading zero will be present only if the BigInteger is zero.
bi - unscaled value of the BigDecimal;
this parameter must not be null.iScale - the positive scale of the BigDecimal (the position
at which a decimal point must be placed in the BigInteger parameter, measured
in decimal digits from the right).java.lang.NumberFormatException - scale is negativepublic BigDecimal(java.lang.String s)
s - String representation of BigDecimal.java.lang.NumberFormatException - s is not a valid representation
of a BigDecimal.BigDecimal(String s, char cDecimalSeparator)
public BigDecimal(java.lang.String s,
char cDecimalSeparator)
In summary, the String representation consists of an optional minus sign followed by a sequence of zero or more decimal digits, optionally followed by a fraction, and, in addition to what java.math.BigDecimal accepts as numbers; this number may be followed by an exponent in the usual notation, to know an 'E' or 'e' followed by a signed or unsigned integer (the exponent proper) that indicates how the decimal point will be shifted (i.e. by multiplying the number by the power of ten indicated by the exponent). The fraction consists of of a decimal point followed by zero or more decimal digits. The String must contain at least one digit in the integer or fractional part. It follows conventional syntax, and may not contain any extraneous characters (whitespace or grouping characters, for example).
Some valid strings from which a BigDecimal might be constructed are:
"0" // Zero
"0.00" // Zero (with 2 decimal places)
"2318" // A whole ('natural') number
"2318." // The same as 2318
"-83" // A signed whole number
"99.95" // Two decimal places
"+0.012" // Plus sign is allowed, but has no implications
".012" // The same as 0.012
"0.67E-3" // Exponential notation for 0.00067
"7e+5" // Exponential notation for 700000, with lowercase 'e'
"7E5" // Exponential notation for 700000; note that the '+' in exponent is not obligatory
If the String uses exponential notation (that is, includes an e or an
E), then the BigDecimal number will be expressed in SCIENTIFIC notation
when rendered by toString() (where the power of ten is adjusted so there is a
single non-zero digit to the left of the decimal point); in this case
if the number is zero then it will be expressed as the single digit 0,
and if non-zero it will have an exponent unless that exponent would be 0.
The exponent both in the String constructed from and the String the BigDecimal
would be rendered to by toString() is limited to the range
-1000000000 (-10E9) < exponent < 1000000000 (10E9).Any digits in the parameter must be decimal; that is, Character.digit(c, 10) (where c is the character in question) should not return -1.
Note: This constructor is not in com.ibm.math.BigDecimal.
s - String representation of BigDecimal.cDecimalSeparator - the decimal separator used in s.
The decimal separator for a locale can be retrieved from a
java.text.DecimalFormatSymbols object for that locale.java.lang.NumberFormatException - s is not a valid representation
of a BigDecimal.BigDecimal(String s),
"Character.digit(char, int)",
MathContext.PLAIN,
MathContext.SCIENTIFIC,
MathContext.ENGINEERINGpublic BigDecimal(byte[] abAsciiDigits)
abAsciiDigits - The byte[] array containing the number to be converted, in ASCII format
(i.e. 0 represented by 0x30 or (byte)'0', 1 by 0x31 or (byte)'1', and soforth).java.lang.NumberFormatException - abAsciiDigits is not a valid representation
of a BigDecimal.BigDecimal(byte[] abAsciiDigits, char cDecimalSeparator)
public BigDecimal(byte[] abAsciiDigits,
char cDecimalSeparator)
Constructs a BigDecimal as decscribed for the BigDecimal(java.lang.String) constructor, but using this constructor is faster if the String is available as an array of ASCII character bytes (e.g. when reading from a file).
Note: This constructor is not in com.ibm.math.BigDecimal.
abAsciiDigits - The byte[] array containing the number to be converted, in ASCII format
(i.e. 0 represented by 0x30 or (byte)'0', 1 by 0x31 or (byte)'1', and soforth).cDecimalSeparator - the decimal separator used in abAsciiDigits.
The decimal separator for a locale can be retrieved from a
java.text.DecimalFormatSymbols object for that locale.java.lang.NumberFormatException - abAsciiDigits is not a valid representation
of a BigDecimal.BigDecimal(byte[] abAsciiDigits)
public BigDecimal(byte[] abAsciiDigits,
int iStart,
int iLength)
abAsciiDigits - The byte[] array containing the number to be converted, in ASCII format
(i.e. 0 represented by 0x30 or (byte)'0', 1 by 0x31 or (byte)'1', and soforth).iStart - The int offset into the array of the start of the number
to be converted.iLength - The int length of the number; the subarray indicated
by iStart and iLength must be wholly contained within abAsciiDigits.java.lang.NumberFormatException - the subarray of abAsciiDigits is not a valid representation
of a BigDecimal.BigDecimal(byte[] abAsciiDigits, int iStart, int iLength, char cDecimalSeparator)
public BigDecimal(byte[] abAsciiDigits,
int iStart,
int iLength,
char cDecimalSeparator)
Constructs a BigDecimal as decscribed for the BigDecimal(java.lang.String) constructor, but using this constructor is faster if the String is available as an array of ASCII character bytes (e.g. when reading from a file).
Note: This constructor is not in com.ibm.math.BigDecimal.
abAsciiDigits - The byte[] array containing the number to be converted, in ASCII format
(i.e. 0 represented by 0x30 or (byte)'0', 1 by 0x31 or (byte)'1', and soforth), in a subarray.iStart - The int offset into the array of the start of the number
to be converted.iLength - The int length of the number; the subarray indicated
by iStart and iLength must be wholly contained within abAsciiDigits.cDecimalSeparator - the decimal separator used in abAsciiDigits.
The decimal separator for a locale can be retrieved from a
java.text.DecimalFormatSymbols object for that locale.java.lang.NumberFormatException - the subarray of abAsciiDigits is not a valid representation
of a BigDecimal.BigDecimal(byte[] abAsciiDigits, int iStart, int iLength)public BigDecimal(char[] ac)
ac - The char[] array containing the number to be converted.java.lang.NumberFormatException - ac is not a valid representation
of a BigDecimal.BigDecimal(char[] ac, char cDecimalSeparator)
public BigDecimal(char[] ac,
char cDecimalSeparator)
Constructs a BigDecimal as decscribed for the BigDecimal(java.lang.String) constructor, but using this constructor is faster if the String is available in character array form.
Note: This constructor is not in com.ibm.math.BigDecimal.
ac - The char[] array containing the number to be converted.cDecimalSeparator - the decimal separator used in ac.
The decimal separator for a locale can be retrieved from a
java.text.DecimalFormatSymbols object for that locale.java.lang.NumberFormatException - ac is not a valid representation
of a BigDecimal.BigDecimal(char[] ac)
public BigDecimal(char[] ac,
int iStart,
int iLength)
ac - The char[] array containing the number to be converted in
the specified subarray.iStart - The int offset into the array of the start of the number
to be converted.iLength - The int length of the number in the array; the subarray indicated
by iStart and iLength must be wholly contained within ac.java.lang.NumberFormatException - the subarray of ac is not a valid representation
of a BigDecimal.BigDecimal(char[] ac, int iStart, int iLength, char cDecimalSeparator)
public BigDecimal(char[] ac,
int iStart,
int iLength,
char cDecimalSeparator)
Constructs a BigDecimal as decscribed for the BigDecimal(java.lang.String) constructor, but using this constructor is faster if the String is available in character array form.
Note: This constructor is not in com.ibm.math.BigDecimal.
ac - The char[] array containing the number to be converted in
the specified subarray.iStart - The int offset into the array of the start of the number
to be converted.iLength - The int length of the number in the array; the subarray indicated
by iStart and iLength must be wholly contained within ac.cDecimalSeparator - the decimal separator used in ac.
The decimal separator for a locale can be retrieved from a
java.text.DecimalFormatSymbols object for that locale.java.lang.NumberFormatException - the subarray of ac is not a valid representation
of a BigDecimal.BigDecimal(char[] ac, int iStart, int iLength)| Method Detail |
public BigDecimal abs()
java.math.BigDecimal compatibility signature, the same as abs(new MathContext(0, MathContext.PLAIN)).
public BigDecimal abs(MathContext mc)
If the current object is zero or positive, then the result of invoking the plus(MathContext) method with the same parameter is returned. Otherwise, the result of invoking the negate(MathContext) method with the same parameter is returned.
mc - The MathContext; must not be null.public BigDecimal add(BigDecimal bd)
java.math.BigDecimal compatibility signature, the same as add(bd, new MathContext(0, MathContext.PLAIN)).
bd - value to be added to this BigDecimal; must not be null.
public BigDecimal add(BigDecimal bd,
MathContext mc)
bd - value to be added to this BigDecimal; must not be null.mc - The MathContext arithmetic settings; must not be null.
public byte byteValueExact()
throws java.lang.ArithmeticException
NOTE
The corresponding method byteValue() is inherited from java.lang.Number.
java.lang.ArithmeticException - if this has a non-zero decimal part,
or will not fit in a byte.public int compareTo(java.lang.Object o)
Note: This method signature implements the Comparable interface of Java 2; the compareTo(BigDecimal, MathContext) method should be used when a context is needed for the comparison.
Note: Even though this method signature implements the Comparable interface of Java 2, it will be present in JDK1.x versions of this class; the only difference being that these versions do not implement the java.lang.Comparable interface, as that isn't in JDK1.x.
compareTo in interface java.lang.Comparableo - Object to which this BigDecimal is to be compared; must not be null.ClassCastException - o is not a BigDecimal.compareTo(be.arci.math.BigDecimal),
Comparablepublic int compareTo(BigDecimal bd)
java.math.BigDecimal compatibility signature, the same as compareTo(bd, new MathContext(0, MathContext.PLAIN)).
Note: A compareTo(Object) method is also provided for implementing the Comparable interface of Java 2.
Two BigDecimals that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=); we suggest performing these comparisons as: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
bd - BigDecimal to which this BigDecimal is to be compared; must not be null.
public int compareTo(BigDecimal bd,
MathContext mc)
Note that, due to the possible re-scaling and rounding specified in mc,
this method allows for 'inexact' comparison, as is so often requested
for floating point values. Please let me know if you see a purpose for
another form of 'inexact' comparison; I was thinking of something like
compareWithinMargin(BigDecimal bdComparand, double percents),
which could return 0 ('equal value')
if this * (1-percents) < bdComparand < this * (1+percents),
else +1 or -1 depending on this < or > bdComparand
bd - The BigDecimal for the right hand side of the comparison; must not be null.mc - The MathContext arithmetic settings; must not be null.public BigDecimal divide(BigDecimal bd)
java.math.BigDecimal compatibility signature, the same as divide(bd, new MathContext(0, MathContext.PLAIN)).
bd - The BigDecimal divisor; must not be null.java.lang.ArithmeticException - bd is zero.
public BigDecimal divide(BigDecimal bd,
int roundingMode)
java.math.BigDecimal compatibility signature, the same as divide(bd, new MathContext(0, MathContext.PLAIN, false, roundingMode)).
bd - The BigDecimal divisor; must not be null.roundingMode - rounding mode to apply.java.lang.ArithmeticException - bd==0, or
roundingMode==ROUND_UNNECESSARY and
this.scale() is insufficient to represent the result
of the division exactly.java.lang.IllegalArgumentException - roundingMode does not
represent a valid rounding mode.MathContext.ROUND_UP,
MathContext.ROUND_DOWN,
MathContext.ROUND_CEILING,
MathContext.ROUND_FLOOR,
MathContext.ROUND_HALF_UP,
MathContext.ROUND_HALF_DOWN,
MathContext.ROUND_HALF_EVEN,
MathContext.ROUND_UNNECESSARY
public BigDecimal divide(BigDecimal bd,
int iScale,
int roundingMode)
java.math.BigDecimal compatibility signature, has no equivalent with MathContext parameter.
bd - The BigDecimal divisor; must not be null.iScale - scale of the BigDecimal quotient to be returned.roundingMode - rounding mode to apply.java.lang.ArithmeticException - bd is zero, iScale is
negative, or roundingMode==ROUND_UNNECESSARY and
this BigDecimal's scale is insufficient to represent the result
of the division exactly.java.lang.IllegalArgumentException - roundingMode does not
represent a valid rounding mode.MathContext.ROUND_UP,
MathContext.ROUND_DOWN,
MathContext.ROUND_CEILING,
MathContext.ROUND_FLOOR,
MathContext.ROUND_HALF_UP,
MathContext.ROUND_HALF_DOWN,
MathContext.ROUND_HALF_EVEN,
MathContext.ROUND_UNNECESSARY
public BigDecimal divide(BigDecimal bd,
MathContext mc)
bd - The BigDecimal divisor; must not be null.mc - The MathContext arithmetic settings; must not be null.java.lang.ArithmeticException - if bd is zero.public BigDecimal[] divideAndRemainder(BigDecimal bd)
Note: This method is not in com.ibm.math.BigDecimal.
The same as divideAndRemainder(bd, new MathContext(0, MathContext.PLAIN)).
bd - The BigDecimal divisor; must not be null.java.lang.ArithmeticException - bd==0divideInteger(BigDecimal),
remainder(BigDecimal)
public BigDecimal[] divideAndRemainder(BigDecimal bd,
MathContext mc)
Note: This method is not in com.ibm.math.BigDecimal.
bd - The BigDecimal divisor; must not be null.mc - The MathContext arithmetic settings; must not be null.java.lang.ArithmeticException - bd==0divideInteger(BigDecimal,MathContext),
remainder(BigDecimal,MathContext)public BigDecimal divideInteger(BigDecimal bd)
The same as divideInteger(bd, new MathContext(0, MathContext.PLAIN)).
bd - The BigDecimal divisor; must not be null.java.lang.ArithmeticException - if bd is zero.
public BigDecimal divideInteger(BigDecimal bd,
MathContext mc)
bd - The BigDecimal divisor; must not be null.mc - The MathContext arithmetic settings; must not be null.java.lang.ArithmeticException - if bd is zero or if the result will not
fit in the number of digits specified for the context.public double doubleValue()
Note: There is not and cannot be a doubleValueExact() method like there is an intValueExact() etc.
doubleValue in class java.lang.Numberpublic boolean equals(java.lang.Object o)
equals in class java.lang.Objecto - Object to which this BigDecimal is to be compared; null is never equal.compareTo(be.arci.math.BigDecimal)public float floatValue()
floatValue in class java.lang.Number
public java.lang.String format(int iIntDigits,
int iDecDigits)
The same as format(iIntDigits, iDecDigits, -1, -1, MathContext.SCIENTIFIC, -1)
This method is provided as a primitive for use by more sophisticated classes, such as DecimalFormat, that can apply locale-sensitive editing of the result. The level of formatting that it provides is a necessary part of the BigDecimal class as it is sensitive to and must follow the calculation and rounding rules for BigDecimal arithmetic. However, if the function is provided elsewhere, it may be removed from this class.
The parameters iIntDigits and iDecDigits specify the number of characters to be used for representing the integer part and decimal part of the result respectively. If either parameter is -1 (which indicates the default action), the number of characters used will be exactly as many as are needed for that part.
iIntDigits, if not -1, must be a positive number (not 0); if it is larger than is needed to contain the integer part, that part is padded on the left with blanks to the requested length. If iIntDigits is not large enough to contain the integer part of the number (including the sign, for negative numbers) an exception is thrown.
iDecDigits, if not -1, must be a non-negative number; if it is not the same size as the decimal part of the number, the number will be rounded (or extended with zeros) to fit. If iDecDigits is 0, the number will be rounded to an integer and will have no decimal point. The rounding mode is MathContext.ROUND_HALF_UP.
Other rounding methods, and the use of exponential notation, can be selected by using format(int,int,int,int,int,int).
iIntDigits - The number of places before the decimal
point, or -1 to use as many digits as needed.iDecDigits - The number of places after the decimal
point, or -1 to use as many digits as needed.java.lang.ArithmeticException - if the number cannot be formatted as
requested.java.lang.IllegalArgumentException - if a parameter is out of
range.toString(),
toCharArray()
public java.lang.String format(int iIntDigits,
int iDecDigits,
int iExpDigits,
int iMaxIntDigits,
int iNot,
int roundingMode)
This method is provided as a primitive for use by more sophisticated classes, such as DecimalFormat, that can apply locale-sensitive editing of the result. The level of formatting that it provides is a necessary part of the BigDecimal class as it is sensitive to and must follow the calculation and rounding rules for BigDecimal arithmetic. However, if the function is provided elsewhere, it may be removed from this class.
The two parameters iIntDigits and iDecDigits specify the number of characters to be used for the integer part and decimal part of the result respectively, as described for format(int,int). If either of these is -1, the number of characters used will be exactly as many as are needed for that part.
The parameters iExpDigits, iMaxIntDigits, and iNot control
the use of exponential notation.
iExpDigits, if not -1, must be a positive number (not 0), and sets the
number of digits after the sign of the exponent to be used for any
exponent part; the requested space is always reserved for an exponent:
if no exponent is needed (for example, if the exponent will be 0) then
iExpDigits+2 blanks are appended to the result. If iExpDigits
is not large enough to contain the exponent, an exception is thrown. The
default action, when iExpDigits is -1, is to use as many digits as needed.
iMaxIntDigits, if not -1, sets the trigger point for use of exponential notation. If, before any rounding, the number of places needed before the decimal point exceeds iMaxIntDigits, or if the absolute value of the result is less than 0.000001 (10E-6), then exponential form will be used. If iMaxIntDigits is -1, exponential notation will never be used. If iMaxIntDigits is 0, exponential notation is always used unless the exponent would be 0.
iNot sets the form of exponential notation (if needed). It may be either MathContext.SCIENTIFIC or MathContext.ENGINEERING. If the engineering notation is requested, up to three digits (plus sign, if negative) may be needed for the integer part of the result (iIntDigits). Otherwise, only one digit (plus sign, if negative) is needed.
roundingMode, selects the rounding algorithm to be used, and must be one of the rounding mode constants of the MathContext class. The default (MathContext.ROUND_HALF_UP) may also be selected by using the value -1. The special value MathContext.ROUND_UNNECESSARY may be used to assert that the formatting discards no non-zero digits; if it does, an ArithmeticException is thrown.
iIntDigits - The number of places before the decimal point, or -1 to use
-1 as many digits as needed.iDecDigits - The number of places after the decimal
point, or -1 to use as many digits as needed.iExpDigits - The number of digits to use for
any exponent, or -1 to use as many digits as needed.iMaxIntDigits - The number of digits before the
decimal point triggering the use of exponential notation; use 0 to force
exponential notation, and use -1 to force plain notation (no
exponential notation).iNot - The form of exponential notation to be
used (MathContext.SCIENTIFIC or MathContext.ENGINEERING).roundingMode - The rounding mode to use. Use -1 for
the default, MathContext.ROUND_HALF_UP.java.lang.ArithmeticException - if the number cannot be formatted as
requested.java.lang.IllegalArgumentException - if a parameter is out of
range.toString(),
toCharArray()public int getDigits()
Note: This method is not in com.ibm.math.BigDecimal.
public int getExponent()
Examples:
"1"getExponent() returns 0 (1E+0)
"14154"getExponent() returns 4 (1.4154E+4)
"0.00000014154"getExponent() returns -7 (1.4154E-7)
"14.154E6"getExponent() returns 7 (1.4154E+7)
scale(),
getDigits()public int hashCode()
hashCode in class java.lang.Objectequals(Object)public int intValue()
To avoid unexpected errors when these conditions occur, use the intValueExact() method.
intValue in class java.lang.Number
public int intValueExact()
throws java.lang.ArithmeticException
java.lang.ArithmeticException - if this has a non-zero decimal part,
or will not fit in an int.public boolean isByteValue()
Note: This method is not in com.ibm.math.BigDecimal.
public boolean isIntValue()
Note: This method is not in com.ibm.math.BigDecimal.
public boolean isLongValue()
Note: This method is not in com.ibm.math.BigDecimal.
public boolean isShortValue()
Note: This method is not in com.ibm.math.BigDecimal.
public long longValue()
To avoid unexpected errors when these conditions occur, use the longValueExact() method.
longValue in class java.lang.Number
public long longValueExact()
throws java.lang.ArithmeticExcepti