- java.lang.Object
-
- java.lang.Number
-
- java.math.BigDecimal
-
- org.tentackle.common.BMoney
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.lang.Comparable<java.math.BigDecimal>
- Direct Known Subclasses:
DMoney
public class BMoney extends java.math.BigDecimal implements java.lang.CloneableMoney value derived from BigDecimal.
A money value has a distinct and fixed scale that cannot change.- Author:
- harald
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description BMoney()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.BMoney(java.lang.String val, int scale)Creates a BMoney value.BMoney(java.math.BigDecimal val)Creates a BMoney from a BigDecimal (userful for SQL)BMoney(java.math.BigInteger intVal, int scale)Creates a BMoney value from a BigInteger.
The scale sets the comma within the given integer.BMoney(BMoney val, int scale)Creates a BMoney from a BMoney (useful to rescale)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BMoneyabsolute()Gets the absolute value.BMoneyadd(BMoney val)Adds a BMoney to this value and returns a new object.protected BMoneyalignScale(BMoney val)Check the scale of the given money value.BMoneyclone()Clones a BMoney value.BMoneydivide(double val)Divides a BMoney by double and returns a new object.doubledivide(BMoney val)Divides a BMoney by this value and returns a new object.BMoneyinvert()Inverts this money value.booleanisNegative()Checks if this money value is negative.booleanisPositive()Checks if this money value is positive.booleanisZero()Checks if this money value is zero.BMoneymultiply(double val)Multiplies this BMoney by a double and returns a new object.BMoneysmallestPositive()Returns the smalles positive value.
Ex.: if scale is 2 --> 0.01 will be returned.BMoneysubtract(BMoney val)Subtracts a BMoney from this value and returns a new object.java.lang.StringtoWords()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, valueOf
-
-
-
-
Constructor Detail
-
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
public BMoney(java.lang.String val, int scale)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
public BMoney(java.math.BigInteger intVal, int scale)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
public BMoney(BMoney val, int scale)
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
public BMoney(java.math.BigDecimal val)
Creates a BMoney from a BigDecimal (userful for SQL)- Parameters:
val- the decimal value
-
BMoney
public BMoney()
Creates a zero BMoney with a scale according to the currency of the current locale.
-
-
Method Detail
-
clone
public BMoney clone()
Clones a BMoney value.- Overrides:
clonein classjava.lang.Object
-
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
public BMoney add(BMoney val)
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
public BMoney subtract(BMoney val)
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
public BMoney multiply(double val)
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
public double divide(BMoney val)
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
public BMoney divide(double val)
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
public BMoney invert()
Inverts this money value.- Returns:
- the negated money value
-
absolute
public BMoney absolute()
Gets the absolute value.- Returns:
- the absolute (positive) value
-
smallestPositive
public BMoney smallestPositive()
Returns the smalles positive value.
Ex.: if scale is 2 --> 0.01 will be returned.- Returns:
- the smalles positive value
-
toWords
public java.lang.String 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
protected BMoney alignScale(BMoney val)
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
-
-