java.lang.Object
java.lang.Number
java.math.BigDecimal
org.tentackle.common.BMoney
- All Implemented Interfaces:
Serializable,Cloneable,Comparable<BigDecimal>
- Direct Known Subclasses:
DMoney
Money value derived from BigDecimal.
A money value has a distinct and fixed scale that cannot change.
A money value has a distinct and fixed scale that cannot change.
- Author:
- harald
- See Also:
-
Field Summary
Fields inherited from class java.math.BigDecimal
ONE, ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UNNECESSARY, ROUND_UP, TEN, TWO, ZERO -
Constructor Summary
ConstructorsConstructorDescriptionBMoney()Creates a zero BMoney with a scale according to the currency of the current locale.BMoney(double val, int scale) Creates a BMoney value.BMoney(int scale) Creates a zero BMoney value.Creates a BMoney value.BMoney(BigDecimal val) Creates a BMoney from a BigDecimal.BMoney(BigInteger intVal, int scale) Creates a BMoney value from a BigInteger.
The scale sets the comma within the given integer.Creates a BMoney from a BMoney (useful to rescale) -
Method Summary
Modifier and TypeMethodDescriptionabsolute()Gets the absolute value.Adds a BMoney to this value and returns a new object.protected BMoneyalignScale(BMoney val) Check the scale of the given money value.clone()Clones a BMoney value.divide(double val) Divides a BMoney by double and returns a new object.doubleDivides a BMoney by this value and returns a new object.invert()Inverts this money value.booleanChecks if this money value is negative.booleanChecks if this money value is positive.booleanisZero()Checks if this money value is zero.multiply(double val) Multiplies this BMoney by a double and returns a new object.Returns the smallest positive value.
Ex.: if scale is 2 --> 0.01 will be returned.Subtracts a BMoney from this value and returns a new object.toWords()Converts a money value to a string of digits as words.
The digits after the comma are ignored.Methods inherited from class java.math.BigDecimal
abs, abs, add, add, byteValueExact, compareTo, divide, divide, divide, divide, divide, divide, divideAndRemainder, divideAndRemainder, divideToIntegralValue, divideToIntegralValue, doubleValue, equals, floatValue, hashCode, intValue, intValueExact, longValue, longValueExact, max, min, movePointLeft, movePointRight, multiply, multiply, negate, negate, plus, plus, pow, pow, precision, remainder, remainder, round, scale, scaleByPowerOfTen, setScale, setScale, setScale, shortValueExact, signum, sqrt, stripTrailingZeros, subtract, subtract, toBigInteger, toBigIntegerExact, toEngineeringString, toPlainString, toString, ulp, unscaledValue, valueOf, valueOf, valueOfMethods inherited from class java.lang.Number
byteValue, shortValue
-
Constructor Details
-
BMoney
public BMoney(double val, int scale) Creates a BMoney value.- Parameters:
val- double-representation of the numberscale- the digits after comma the value should be rounded to
-
BMoney
Creates a BMoney value.- Parameters:
val- the stringscale- the digits after comma the value should be rounded to
-
BMoney
public BMoney(int scale) Creates a zero BMoney value.- Parameters:
scale- the digits after comma
-
BMoney
Creates a BMoney value from a BigInteger.
The scale sets the comma within the given integer.- Parameters:
intVal- the big integerscale- the digits after comma
-
BMoney
Creates a BMoney from a BMoney (useful to rescale)- Parameters:
val- the BMoney valuescale- the digits after comma the value should be rounded to
-
BMoney
Creates a BMoney from a BigDecimal.- Parameters:
val- the decimal value
-
BMoney
public BMoney()Creates a zero BMoney with a scale according to the currency of the current locale.
-
-
Method Details
-
clone
Clones a BMoney value. -
isNegative
public boolean isNegative()Checks if this money value is negative.- Returns:
- true if number is negative
-
isPositive
public boolean isPositive()Checks if this money value is positive.- Returns:
- true if number is positive
-
isZero
public boolean isZero()Checks if this money value is zero.- Returns:
- true if number is zero
-
add
Adds a BMoney to this value and returns a new object. This object remains unchanged.- Parameters:
val- the money value to add- Returns:
- the sum of this and given value
-
subtract
Subtracts a BMoney from this value and returns a new object. This object remains unchanged.- Parameters:
val- the money value to subtract- Returns:
- this minus the given value
-
multiply
Multiplies this BMoney by a double and returns a new object. This object remains unchanged.- Parameters:
val- the double to multiply with- Returns:
- the product of this and given value
-
divide
Divides a BMoney by this value and returns a new object. This object remains unchanged.- Parameters:
val- the money value to multiply- Returns:
- the quotient of this and given value
-
divide
Divides a BMoney by double and returns a new object. This object remains unchanged.- Parameters:
val- the value to divide this BMoney by- Returns:
- the quotient of this and given value
-
invert
Inverts this money value.- Returns:
- the negated money value
-
absolute
Gets the absolute value.- Returns:
- the absolute (positive) value
-
smallestPositive
Returns the smallest positive value.
Ex.: if scale is 2 --> 0.01 will be returned.- Returns:
- the smallest positive value
-
toWords
Converts a money value to a string of digits as words.
The digits after the comma are ignored. Nice to print checks.Example: 234,77 will become "two three four"
- Returns:
- the value as a words
-
alignScale
Check the scale of the given money value. If it does not match the scale of this object, scale it to the current scale of this object.- Parameters:
val- the value to be checked- Returns:
- the re-scaled value or the value itself if no rescaling necessary
-