|
Version 4.0.0
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
ObjectNumber
hirondelle.web4j.model.Decimal
public final class Decimal
Represents a decimal amount.
Decimal amounts are typically used to represent two kinds of items :
Your applications are not obliged to use this class to represent decimal amounts.
You may choose to use BigDecimal instead (perhaps along with an Id
to store a currency, if needed).
This class exists for these reasons :
BigDecimal class
Decimal objects are immutable. Many operations return new Decimal objects.
Many will be surprised that this class does not make any reference to currency. The reason for this is adding currency would render this class a poor building block. Building block objects such as Date, Integer, and so on, are atomic, in the sense of representing a single piece of data. They correspond to a single column in a table, or a single form control. If the currency were included in this class, then it would no longer be atomic, and it could not be treated by WEB4J as any other building block class. However, allowing this class to be treated like any other building block class is highly advantageous.
If a feature needs to explicitly distinguish between multiple currencies
such as US Dollars and Japanese Yen, then a Decimal object
will need to be paired by the caller with a second item representing the
underlying currency (perhaps modeled as an Id).
See the Currency class for more information.
Check.numDecimalsAlways(int) or Check.numDecimalsMax(int) methods.
The init(RoundingMode, int) method is called upon startup.
It takes a parameter which specifies the number of decimal places.
It is used only for rounding the results of times and div operations.
It is not used to validate the number of decimals in
items passed to the Decimal constructor.
As usual, operations can be performed on two items having a different number of decimal places. For example, these operations are valid (using an informal, ad hoc notation) :
10 + 1.23 = 11.23 10.00 + 1.23 = 11.23 10 - 1.23 = 8.77 (10 > 1.23) => trueThis corresponds to typical user expectations.
Note that equals(Object) is unusual in that it is the only method sensitive to the exact
number of decimal places, while eq(Decimal) is not. That is,
10.equals(10.00) => false 10.eq(10.00) => true
The times and div operations are different, since the result can have a larger number of decimals than usual. For example, when dealing with US Dollars, the result
$10.00 x 0.1256 = $1.256has more than two decimals. In such cases, this class will round results of multiplication and division, using the setting passed to
init(RoundingMode, int).
This policy likely conforms to the expectations of most end users.
The times(long) method is an exception to this rule.
The init(RoundingMode, int) method takes two parameters.
One controls the rounding policy, and the other controls the number of decimals to use by default.
When the default number of decimals needs to be overridden, you must use the
changeTimesDivDecimals(int) method.
Example :
if ( amount.lt(hundred) ) {
cost = amount.times(price);
}
In short, the double versions are best suited when using hard-coded values for factors and divisors, while the Decimal versions are suited for the (more common) case of using values coming from the database or user input.
Using Decimal is the preferred form, since there are many pitfalls associated with double. The double form has been retained since it's more convenient for the caller in some cases, and one of the goals of this class is to allow terse mathematical expressions.
Number. An immediate benefit of this is that it allows JSTL's
fmt tags to render Decimal objects in the usual way.
| Field Summary | |
|---|---|
static Decimal |
ZERO
Zero Decimal amount. |
| Constructor Summary | |
|---|---|
Decimal(BigDecimal aAmount)
Full constructor. |
|
| Method Summary | |
|---|---|
Decimal |
abs()
Return the absolute value of the amount. |
Decimal |
changeTimesDivDecimals(int aTimesDivDecimals)
Override the default number of decimals retained in times and div operations. |
int |
compareTo(Decimal aThat)
Implements the Comparable interface. |
Decimal |
div(Decimal aDivisor)
Divide this Decimal by an non-integral divisor. |
Decimal |
div(double aDivisor)
Divide this Decimal by an non-integral divisor. |
Decimal |
div(long aDivisor)
Divide this Decimal by an integral divisor. |
double |
doubleValue()
Required by Number. |
boolean |
eq(Decimal aThat)
Equals (insensitive to number of decimals). |
boolean |
equals(Object aThat)
Equals (sensitive to number of decimals). |
float |
floatValue()
Required by Number. |
static Decimal |
from(String aAmount)
Convenience factory method. |
BigDecimal |
getAmount()
Return the amount passed to the constructor. |
int |
getNumDecimals()
Return the number of decimals in this value. |
static RoundingMode |
getRoundingStyle()
Return the rounding style passed to the init method. |
int |
getTimesDivDecimals()
Return the number of decimals to be retained by times and div operations. |
static int |
getTimesDivDecimalsDefault()
Return the number of decimals passed to the init method. |
boolean |
gt(Decimal aThat)
Greater than. |
boolean |
gteq(Decimal aThat)
Greater than or equal to. |
int |
hashCode()
|
static void |
init(RoundingMode aRounding,
int aNumDecimalsForTimesDiv)
Set default values for the rounding style, and the maximum number of decimals to use when calculating results of times and div operations. |
int |
intValue()
Required by Number. |
boolean |
isMinus()
Return true only if the amount is negative. |
boolean |
isPlus()
Return true only if the amount is positive. |
boolean |
isZero()
Return true only if the amount is zero. |
long |
longValue()
Required by Number. |
boolean |
lt(Decimal aThat)
Less than. |
boolean |
lteq(Decimal aThat)
Less than or equal to. |
Decimal |
minus(Decimal aThat)
Subtract aThat Decimal from this Decimal. |
Decimal |
negate()
Return the amount x (-1). |
Decimal |
plus(Decimal aThat)
Add aThat Decimal to this Decimal. |
Decimal |
round()
Round to an integer. |
Decimal |
round(int aNumberOfDecimals)
Round to 0 or more decimal places. |
static Decimal |
sum(Collection<Decimal> aDecimals)
Sum a collection of Decimal objects. |
Decimal |
times(Decimal aFactor)
Multiply this Decimal by an non-integral factor (having a decimal point). |
Decimal |
times(double aFactor)
Multiply this Decimal by an non-integral factor (having a decimal point). |
Decimal |
times(long aFactor)
Multiply this Decimal by an integral factor. |
String |
toString()
Renders this Decimal in a style suitable for debugging. |
| Methods inherited from class Number |
|---|
byteValue, shortValue |
| Methods inherited from class Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final Decimal ZERO
Like BigDecimal.ZERO, this item has no explicit decimal.
In most cases that will not matter, since only the equals(Object) method is sensitive to
exact decimals. All other methods, including eq(Decimal), are not sensitive to exact decimals.
| Constructor Detail |
|---|
public Decimal(BigDecimal aAmount)
aAmount - required, can be positive or negative.
Any number of decimals. The value of BigDecimal.scale() cannot
be negative.| Method Detail |
|---|
public static void init(RoundingMode aRounding,
int aNumDecimalsForTimesDiv)
This method is called by the framework upon startup.
The recommended rounding style is RoundingMode.HALF_EVEN, also called
banker's rounding. That rounding style introduces the least bias.
aRounding - defines how all numbers are rounded by this class. This rounding
style is set once, and cannot be overridden for individual Decimal objects.aNumDecimalsForTimesDiv - number of decimals for results of
times and div operations. Must be 0 or more.
Taking the example of US Dollars, this setting would usually be '2'.public static RoundingMode getRoundingStyle()
public static int getTimesDivDecimalsDefault()
public static Decimal from(String aAmount)
Instead of :
Decimal decimal = new Decimal(new BigDecimal("100"));
one may instead use :
Decimal decimal = Decimal.from("100");
which is a bit more legible. This is especially useful when you need to define
specific minimum and maximum values used in validation.
public Decimal changeTimesDivDecimals(int aTimesDivDecimals)
init(RoundingMode, int).
The changeTimesDivDecimals method is called when that default is not
appropriate.
This method will often be used when modeling physical measurements such as temperature, distance, and so on, where the number of decimals can vary according to context.
public BigDecimal getAmount()
public int getTimesDivDecimals()
See changeTimesDivDecimals(int) for a way of altering this value from the default.
public int getNumDecimals()
For validating the number of decimals in user input, you are highly encouraged to
use Check.numDecimalsAlways(int) or Check.numDecimalsMax(int).
public boolean isPlus()
public boolean isMinus()
public boolean isZero()
public boolean eq(Decimal aThat)
That is, 10 and 10.00 are considered equal by this method.
Return true only if the amounts are equal.
This method is not synonymous with the equals method,
since the equals(Object) method is sensitive to the exact number of decimal places.
public boolean gt(Decimal aThat)
Return true only if 'this' amount is greater than 'that' amount.
public boolean gteq(Decimal aThat)
Return true only if 'this' amount is greater than or equal to 'that' amount.
public boolean lt(Decimal aThat)
Return true only if 'this' amount is less than 'that' amount.
public boolean lteq(Decimal aThat)
Return true only if 'this' amount is less than or equal to 'that' amount.
public Decimal plus(Decimal aThat)
public Decimal minus(Decimal aThat)
public static Decimal sum(Collection<Decimal> aDecimals)
aDecimals - collection of Decimal objects.
If the collection is empty, then a zero value is returned.public Decimal times(long aFactor)
The number of decimals in the return value is the same as the number of decimals of 'this' Decimal. For example,
10 x 2 = 20 10.00 x 2 = 20.00This conforms to most user's expectations. This behavior is slightly different from the other times methods.
public Decimal times(Decimal aFactor)
The number of decimals of the result is taken from
getTimesDivDecimals().
public Decimal times(double aFactor)
The number of decimals of the result is taken from
getTimesDivDecimals().
Consider using times(Decimal) as the preferred alternative.
public Decimal div(long aDivisor)
The number of decimals of the result is taken from
getTimesDivDecimals().
public Decimal div(Decimal aDivisor)
The number of decimals of the result is taken from
getTimesDivDecimals().
public Decimal div(double aDivisor)
The number of decimals of the result is taken from
getTimesDivDecimalsDefault().
Consider using div(Decimal) as the preferred alternative.
public Decimal abs()
public Decimal negate()
public Decimal round()
Uses getRoundingStyle().
public Decimal round(int aNumberOfDecimals)
Uses getRoundingStyle().
public String toString()
Returns the amount in the format defined by BigDecimal.toPlainString().
toString in class Objectpublic boolean equals(Object aThat)
That is, 10 and 10.00 are not considered equal by this method.
This implementation imitates BigDecimal.equals(java.lang.Object),
which is also sensitive to the number of decimals (or 'scale').
See eq(Decimal) as well.
equals in class Objectpublic int hashCode()
hashCode in class Objectpublic int compareTo(Decimal aThat)
Comparable interface.
Recommended that you use the other methods such as eq(Decimal), lt(Decimal), and
so on, since they have greater clarity and concision.
compareTo in interface Comparable<Decimal>public double doubleValue()
Number.
Use of floating point data is highly discouraged. This method is provided only because it's required by Number.
doubleValue in class Numberpublic float floatValue()
Number.
Use of floating point data is highly discouraged. This method is provided only because it's required by Number.
floatValue in class Numberpublic int intValue()
Number.
intValue in class Numberpublic long longValue()
Number.
longValue in class Number
|
Version 4.0.0
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||