public class Price extends BigDecimal
| Modifier and Type | Field and Description |
|---|---|
static Price |
ONE |
static Price |
TEN |
static Price |
ZERO |
ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UNNECESSARY, ROUND_UP| Constructor and Description |
|---|
Price(BigDecimal bigDecimal) |
Price(BigInteger val)
Translates a
BigInteger into a BigDecimal. |
Price(BigInteger unscaledVal,
int scale)
Translates a
BigInteger unscaled value and an
int scale into a BigDecimal. |
Price(BigInteger unscaledVal,
int scale,
MathContext mc)
Translates a
BigInteger unscaled value and an
int scale into a BigDecimal, with rounding
according to the context settings. |
Price(BigInteger val,
MathContext mc)
Translates a
BigInteger into a BigDecimal
rounding according to the context settings. |
Price(char[] in)
Translates a character array representation of a
BigDecimal into a BigDecimal, accepting the
same sequence of characters as the Price(String)
constructor. |
Price(char[] in,
int offset,
int len)
Translates a character array representation of a
BigDecimal into a BigDecimal, accepting the
same sequence of characters as the Price(String)
constructor, while allowing a sub-array to be specified. |
Price(char[] in,
int offset,
int len,
MathContext mc)
Translates a character array representation of a
BigDecimal into a BigDecimal, accepting the
same sequence of characters as the Price(String)
constructor, while allowing a sub-array to be specified and
with rounding according to the context settings. |
Price(char[] in,
MathContext mc)
Translates a character array representation of a
BigDecimal into a BigDecimal, accepting the
same sequence of characters as the Price(String)
constructor and with rounding according to the context
settings. |
Price(double val)
Translates a
double into a BigDecimal which
is the exact decimal representation of the double's
binary floating-point value. |
Price(double val,
MathContext mc)
Translates a
double into a BigDecimal, with
rounding according to the context settings. |
Price(int val)
Translates an
int into a BigDecimal. |
Price(int val,
MathContext mc)
Translates an
int into a BigDecimal, with
rounding according to the context settings. |
Price(long val)
Translates a
long into a BigDecimal. |
Price(long val,
MathContext mc)
Translates a
long into a BigDecimal, with
rounding according to the context settings. |
Price(String val)
Translates the string representation of a
BigDecimal
into a BigDecimal. |
Price(String val,
MathContext mc)
Translates the string representation of a
BigDecimal
into a BigDecimal, accepting the same strings as the
Price(String) constructor, with rounding
according to the context settings. |
| Modifier and Type | Method and Description |
|---|---|
Price |
abs()
Returns a
BigDecimal whose value is the absolute value
of this BigDecimal, and whose scale is
this.scale(). |
Price |
add(BigDecimal augend)
Returns a
BigDecimal whose value is (this +
augend), and whose scale is max(this.scale(),
augend.scale()). |
Price |
divide(BigDecimal divisor)
Returns a
BigDecimal whose value is (this /
divisor), and whose preferred scale is (this.scale() -
divisor.scale()); if the exact quotient cannot be
represented (because it has a non-terminating decimal
expansion) an ArithmeticException is thrown. |
Price |
max(BigDecimal val)
Returns the maximum of this
BigDecimal and val. |
Price |
min(BigDecimal val)
Returns the minimum of this
BigDecimal and
val. |
Price |
multiply(BigDecimal multiplicand)
Returns a
BigDecimal whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()). |
Price |
negate()
Returns a
BigDecimal whose value is (-this),
and whose scale is this.scale(). |
Price |
plus()
Returns a
BigDecimal whose value is (+this), and whose
scale is this.scale(). |
Price |
subtract(BigDecimal subtrahend)
Returns a
BigDecimal whose value is (this -
subtrahend), and whose scale is max(this.scale(),
subtrahend.scale()). |
abs, add, byteValueExact, compareTo, divide, divide, divide, divide, divide, divideAndRemainder, divideAndRemainder, divideToIntegralValue, divideToIntegralValue, doubleValue, equals, floatValue, hashCode, intValue, intValueExact, longValue, longValueExact, movePointLeft, movePointRight, multiply, negate, plus, pow, pow, precision, remainder, remainder, round, scale, scaleByPowerOfTen, setScale, setScale, setScale, shortValueExact, signum, stripTrailingZeros, subtract, toBigInteger, toBigIntegerExact, toEngineeringString, toPlainString, toString, ulp, unscaledValue, valueOf, valueOf, valueOfbyteValue, shortValuepublic static final Price ONE
public static final Price TEN
public static final Price ZERO
public Price(char[] in,
int offset,
int len)
BigDecimal into a BigDecimal, accepting the
same sequence of characters as the Price(String)
constructor, while allowing a sub-array to be specified.
Note that if the sequence of characters is already available
within a character array, using this constructor is faster than
converting the char array to string and using the
BigDecimal(String) constructor .
in - char array that is the source of characters.offset - first character in the array to inspect.len - number of characters to consider.NumberFormatException - if in is not a valid
representation of a BigDecimal or the defined subarray
is not wholly within in.public Price(char[] in,
int offset,
int len,
MathContext mc)
BigDecimal into a BigDecimal, accepting the
same sequence of characters as the Price(String)
constructor, while allowing a sub-array to be specified and
with rounding according to the context settings.
Note that if the sequence of characters is already available
within a character array, using this constructor is faster than
converting the char array to string and using the
BigDecimal(String) constructor .
in - char array that is the source of characters.offset - first character in the array to inspect.len - number of characters to consider..mc - the context to use.ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.NumberFormatException - if in is not a valid
representation of a BigDecimal or the defined subarray
is not wholly within in.public Price(char[] in)
BigDecimal into a BigDecimal, accepting the
same sequence of characters as the Price(String)
constructor.
Note that if the sequence of characters is already available
as a character array, using this constructor is faster than
converting the char array to string and using the
BigDecimal(String) constructor .
in - char array that is the source of characters.NumberFormatException - if in is not a valid
representation of a BigDecimal.public Price(char[] in,
MathContext mc)
BigDecimal into a BigDecimal, accepting the
same sequence of characters as the Price(String)
constructor and with rounding according to the context
settings.
Note that if the sequence of characters is already available
as a character array, using this constructor is faster than
converting the char array to string and using the
BigDecimal(String) constructor .
in - char array that is the source of characters.mc - the context to use.ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.NumberFormatException - if in is not a valid
representation of a BigDecimal.public Price(String val)
BigDecimal
into a BigDecimal. The string representation consists
of an optional sign, '+' ( '\u002B') or
'-' ('\u002D'), followed by a sequence of
zero or more decimal digits ("the integer"), optionally
followed by a fraction, optionally followed by an exponent.
The fraction consists of a decimal point followed by zero or more decimal digits. The string must contain at least one digit in either the integer or the fraction. The number formed by the sign, the integer and the fraction is referred to as the significand.
The exponent consists of the character 'e'
('\u0065') or 'E' ('\u0045')
followed by one or more decimal digits. The value of the
exponent must lie between -Integer.MAX_VALUE (Integer.MIN_VALUE+1) and Integer.MAX_VALUE, inclusive.
More formally, the strings this constructor accepts are described by the following grammar:
- BigDecimalString:
- Signopt Significand Exponentopt
- Sign:
+-- Significand:
- IntegerPart
.FractionPartopt.FractionPart- IntegerPart
- IntegerPart:
- Digits
- FractionPart:
- Digits
- Exponent:
- ExponentIndicator SignedInteger
- ExponentIndicator:
eE- SignedInteger:
- Signopt Digits
- Digits:
- Digit
- Digits Digit
- Digit:
- any character for which
Character.isDigit(char)returnstrue, including 0, 1, 2 ...
The scale of the returned BigDecimal will be the
number of digits in the fraction, or zero if the string
contains no decimal point, subject to adjustment for any
exponent; if the string contains an exponent, the exponent is
subtracted from the scale. The value of the resulting scale
must lie between Integer.MIN_VALUE and
Integer.MAX_VALUE, inclusive.
The character-to-digit mapping is provided by Character.digit(char, int) set to convert to radix 10. The
String may not contain any extraneous characters (whitespace,
for example).
Examples:
The value of the returned BigDecimal is equal to
significand × 10 exponent.
For each string on the left, the resulting representation
[BigInteger, scale] is shown on the right.
"0" [0,0] "0.00" [0,2] "123" [123,0] "-123" [-123,0] "1.23E3" [123,-1] "1.23E+3" [123,-1] "12.3E+7" [123,-6] "12.0" [120,1] "12.3" [123,1] "0.00123" [123,5] "-1.23E-12" [-123,14] "1234.5E-4" [12345,5] "0E+7" [0,-7] "-0" [0,0]
Note: For values other than float and
double NaN and ±Infinity, this constructor is
compatible with the values returned by Float.toString(float)
and Double.toString(double). This is generally the preferred
way to convert a float or double into a
BigDecimal, as it doesn't suffer from the unpredictability of
the Price(double) constructor.
val - String representation of BigDecimal.NumberFormatException - if val is not a valid
representation of a BigDecimal.public Price(String val, MathContext mc)
BigDecimal
into a BigDecimal, accepting the same strings as the
Price(String) constructor, with rounding
according to the context settings.val - string representation of a BigDecimal.mc - the context to use.ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.NumberFormatException - if val is not a valid
representation of a BigDecimal.public Price(double val)
double into a BigDecimal which
is the exact decimal representation of the double's
binary floating-point value. The scale of the returned
BigDecimal is the smallest value such that
(10scale × val) is an integer.
Notes:
new BigDecimal(0.1) in
Java creates a BigDecimal which is exactly equal to
0.1 (an unscaled value of 1, with a scale of 1), but it is
actually equal to
0.1000000000000000055511151231257827021181583404541015625.
This is because 0.1 cannot be represented exactly as a
double (or, for that matter, as a binary fraction of
any finite length). Thus, the value that is being passed
in to the constructor is not exactly equal to 0.1,
appearances notwithstanding.
String constructor, on the other hand, is
perfectly predictable: writing new BigDecimal("0.1")
creates a BigDecimal which 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.
double must be used as a source for a
BigDecimal, note that this constructor provides an
exact conversion; it does not give the same result as
converting the double to a String using the
Double.toString(double) method and then using the
Price(String) constructor. To get that result,
use the static BigDecimal.valueOf(double) method.
val - double value to be converted to
BigDecimal.NumberFormatException - if val is infinite or NaN.public Price(double val,
MathContext mc)
double into a BigDecimal, with
rounding according to the context settings. The scale of the
BigDecimal is the smallest value such that
(10scale × val) is an integer.
The results of this constructor can be somewhat unpredictable
and its use is generally not recommended; see the notes under
the Price(double) constructor.
val - double value to be converted to
BigDecimal.mc - the context to use.ArithmeticException - if the result is inexact but the
RoundingMode is UNNECESSARY.NumberFormatException - if val is infinite or NaN.public Price(BigInteger val)
BigInteger into a BigDecimal.
The scale of the BigDecimal is zero.val - BigInteger value to be converted to
BigDecimal.public Price(BigInteger val, MathContext mc)
BigInteger into a BigDecimal
rounding according to the context settings. The scale of the
BigDecimal is zero.val - BigInteger value to be converted to
BigDecimal.mc - the context to use.ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.public Price(BigInteger unscaledVal, int scale)
BigInteger unscaled value and an
int scale into a BigDecimal. The value of
the BigDecimal is
(unscaledVal × 10-scale).unscaledVal - unscaled value of the BigDecimal.scale - scale of the BigDecimal.public Price(BigInteger unscaledVal, int scale, MathContext mc)
BigInteger unscaled value and an
int scale into a BigDecimal, with rounding
according to the context settings. The value of the
BigDecimal is (unscaledVal ×
10-scale), rounded according to the
precision and rounding mode settings.unscaledVal - unscaled value of the BigDecimal.scale - scale of the BigDecimal.mc - the context to use.ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.public Price(int val)
int into a BigDecimal. The
scale of the BigDecimal is zero.val - int value to be converted to
BigDecimal.public Price(int val,
MathContext mc)
int into a BigDecimal, with
rounding according to the context settings. The scale of the
BigDecimal, before any rounding, is zero.val - int value to be converted to BigDecimal.mc - the context to use.ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.public Price(long val)
long into a BigDecimal. The
scale of the BigDecimal is zero.val - long value to be converted to BigDecimal.public Price(long val,
MathContext mc)
long into a BigDecimal, with
rounding according to the context settings. The scale of the
BigDecimal, before any rounding, is zero.val - long value to be converted to BigDecimal.mc - the context to use.ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.public Price(BigDecimal bigDecimal)
public Price add(BigDecimal augend)
BigDecimal whose value is (this +
augend), and whose scale is max(this.scale(),
augend.scale()).add in class BigDecimalaugend - value to be added to this BigDecimal.this + augendpublic Price multiply(BigDecimal multiplicand)
BigDecimal whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()).multiply in class BigDecimalmultiplicand - value to be multiplied by this BigDecimal.this * multiplicandpublic Price subtract(BigDecimal subtrahend)
BigDecimal whose value is (this -
subtrahend), and whose scale is max(this.scale(),
subtrahend.scale()).subtract in class BigDecimalsubtrahend - value to be subtracted from this BigDecimal.this - subtrahendpublic Price divide(BigDecimal divisor)
BigDecimal whose value is (this /
divisor), and whose preferred scale is (this.scale() -
divisor.scale()); if the exact quotient cannot be
represented (because it has a non-terminating decimal
expansion) an ArithmeticException is thrown.divide in class BigDecimaldivisor - value by which this BigDecimal is to be divided.this / divisorArithmeticException - if the exact quotient does not have a
terminating decimal expansionpublic Price abs()
BigDecimal whose value is the absolute value
of this BigDecimal, and whose scale is
this.scale().abs in class BigDecimalabs(this)public Price negate()
BigDecimal whose value is (-this),
and whose scale is this.scale().negate in class BigDecimal-this.public Price plus()
BigDecimal whose value is (+this), and whose
scale is this.scale().
This method, which simply returns this BigDecimal
is included for symmetry with the unary minus method negate().
plus in class BigDecimalthis.negate()public Price min(BigDecimal val)
BigDecimal and
val.min in class BigDecimalval - value with which the minimum is to be computed.BigDecimal whose value is the lesser of this
BigDecimal and val. If they are equal,
as defined by the compareTo
method, this is returned.BigDecimal.compareTo(java.math.BigDecimal)public Price max(BigDecimal val)
BigDecimal and val.max in class BigDecimalval - value with which the maximum is to be computed.BigDecimal whose value is the greater of this
BigDecimal and val. If they are equal,
as defined by the compareTo
method, this is returned.BigDecimal.compareTo(java.math.BigDecimal)Copyright © 2015. All rights reserved.