Class 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.Cloneable
    Money value derived from BigDecimal.
    A money value has a distinct and fixed scale that cannot change.
    Author:
    harald
    See Also:
    Serialized Form
    • 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, ZERO
    • 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
      BMoney absolute()
      Gets the absolute value.
      BMoney add​(BMoney val)
      Adds a BMoney to this value and returns a new object.
      protected BMoney alignScale​(BMoney val)
      Check the scale of the given money value.
      BMoney clone()
      Clones a BMoney value.
      BMoney divide​(double val)
      Divides a BMoney by double and returns a new object.
      double divide​(BMoney val)
      Divides a BMoney by this value and returns a new object.
      BMoney invert()
      Inverts this money value.
      boolean isNegative()
      Checks if this money value is negative.
      boolean isPositive()
      Checks if this money value is positive.
      boolean isZero()
      Checks if this money value is zero.
      BMoney multiply​(double val)
      Multiplies this BMoney by a double and returns a new object.
      BMoney smallestPositive()
      Returns the smalles positive value.
      Ex.: if scale is 2 --> 0.01 will be returned.
      BMoney subtract​(BMoney val)
      Subtracts a BMoney from this value and returns a new object.
      java.lang.String 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, valueOf
      • Methods inherited from class java.lang.Number

        byteValue, shortValue
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • BMoney

        public BMoney​(double val,
                      int scale)
        Creates a BMoney value.
        Parameters:
        val - double-representation of the number
        scale - 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 string
        scale - 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 integer
        scale - the digits after comma
      • BMoney

        public BMoney​(BMoney val,
                      int scale)
        Creates a BMoney from a BMoney (useful to rescale)
        Parameters:
        val - the BMoney value
        scale - 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:
        clone in class java.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