com.knowgate.math
Class Money

java.lang.Object
  extended by java.lang.Number
      extended by java.math.BigDecimal
          extended by com.knowgate.math.Money
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<java.math.BigDecimal>

public class Money
extends java.math.BigDecimal

Combination of BigDecimal with Currency Sign

This class handles money amounts that include a currency sign

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
Money(java.math.BigDecimal oVal, CurrencyCode oCur)
           
Money(java.math.BigDecimal oVal, java.lang.String sCur)
           
Money(java.math.BigInteger oVal, CurrencyCode oCur)
           
Money(java.math.BigInteger oVal, java.lang.String sCur)
           
Money(double dVal, CurrencyCode oCur)
          Constructor
Money(double dVal, java.lang.String sCur)
          Constructor
Money(Money oVal)
          Constructor that makes a copy from another Money value
Money(java.lang.String sVal, CurrencyCode oCur)
          Constructor
Money(java.lang.String sVal, java.lang.String sCur)
          Constructor
 
Method Summary
 Money add(Money oMny)
          Add two money amounts The return value is in the currency of this object.
 int compareTo(Money oMny)
           
 Money convertTo(CurrencyCode oTarget)
          Convert this money to another currency using a web service for finding the conversion ratio
 Money convertTo(CurrencyCode oTarget, java.math.BigDecimal oRatio)
          Convert this money to another currency
 Money convertTo(java.lang.String sTarget)
          Convert this money to another currency using a web service for finding the conversion ratio
 Money convertTo(java.lang.String sTarget, java.math.BigDecimal oRatio)
          Convert this money to another currency
 CurrencyCode currencyCode()
           
 java.lang.String format(java.lang.String sLanguage, java.lang.String sCountry)
          Format a BigDecimal as a String following the rules for an specific language and country
static boolean isMoney(java.lang.String sVal)
          Checks whether the given string can be parsed as a valid Money value Both comma and dot are allowed as either thousands or decimal delimiters.
 Money max(Money oMny)
           
 Money min(Money oMny)
           
static Money parse(java.lang.String sVal)
           
 Money round2()
          Rounds a BigDecimal value to two decimals
 Money subtract(Money oMny)
          Subtract two money amounts The return value is in the currency of this object.
 java.lang.String toString()
           
 
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, stripTrailingZeros, subtract, subtract, toBigInteger, toBigIntegerExact, toEngineeringString, toPlainString, ulp, unscaledValue, valueOf, valueOf, valueOf
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Money

public Money(Money oVal)
Constructor that makes a copy from another Money value


Money

public Money(java.lang.String sVal,
             CurrencyCode oCur)
      throws java.lang.NumberFormatException
Constructor

Parameters:
sVal - String Numeric value in US decimal format (using dot as decimal delimiter)
oCur - CurrencyCode
Throws:
java.lang.NumberFormatException

Money

public Money(java.lang.String sVal,
             java.lang.String sCur)
      throws java.lang.NumberFormatException,
             java.lang.IllegalArgumentException
Constructor

Parameters:
sVal - String Numeric value in US decimal format (using dot as decimal delimiter)
sCur - String Currency alphanumeric code {"USD", "EUR", etc.}
Throws:
java.lang.NumberFormatException
java.lang.IllegalArgumentException

Money

public Money(double dVal,
             CurrencyCode oCur)
Constructor

Parameters:
dVal - double
oCur - CurrencyCode

Money

public Money(double dVal,
             java.lang.String sCur)
      throws java.lang.IllegalArgumentException
Constructor

Parameters:
dVal - double
sCur - String Currency alphanumeric code {"USD", "EUR", etc.}
Throws:
java.lang.IllegalArgumentException

Money

public Money(java.math.BigDecimal oVal,
             CurrencyCode oCur)

Money

public Money(java.math.BigDecimal oVal,
             java.lang.String sCur)

Money

public Money(java.math.BigInteger oVal,
             CurrencyCode oCur)

Money

public Money(java.math.BigInteger oVal,
             java.lang.String sCur)
Method Detail

currencyCode

public CurrencyCode currencyCode()

compareTo

public int compareTo(Money oMny)

max

public Money max(Money oMny)

min

public Money min(Money oMny)

add

public Money add(Money oMny)
          throws java.lang.NullPointerException

Add two money amounts

The return value is in the currency of this object. If the added amount does not have the same currency then it is converted by calling a web service before performing addition. The scale of the returned value is max(this.scale(), oMny.scale()).

Returns:
this.value + (to this currency) oMny.value
Throws:
java.lang.NullPointerException

subtract

public Money subtract(Money oMny)
               throws java.lang.NullPointerException

Subtract two money amounts

The return value is in the currency of this object. If the added amount does not have the same currency then it is converted by calling a web service before performing addition. The scale of the returned value is max(this.scale(), oMny.scale()).

Returns:
this.value - (to this currency) oMny.value
Throws:
java.lang.NullPointerException

isMoney

public static boolean isMoney(java.lang.String sVal)

Checks whether the given string can be parsed as a valid Money value

Both comma and dot are allowed as either thousands or decimal delimiters. If there is only a comma or a dot then it is assumed to be de decimal delimiter. If both comma and dot are present, then the leftmost of them is assumed to be the thousands delimiter and the rightmost is the decimal delimiter. Any letters and currency symbols {€$£¤¢¥#ƒ&} are ignored


parse

public static Money parse(java.lang.String sVal)
                   throws java.lang.NullPointerException,
                          java.lang.IllegalArgumentException,
                          java.lang.NumberFormatException
Throws:
java.lang.NullPointerException
java.lang.IllegalArgumentException
java.lang.NumberFormatException

round2

public Money round2()
Rounds a BigDecimal value to two decimals

Returns:
BigDecimal

convertTo

public Money convertTo(CurrencyCode oTarget,
                       java.math.BigDecimal oRatio)
                throws java.lang.NullPointerException

Convert this money to another currency

Parameters:
oTarget - Target CurrencyCode
oRatio - BigDecimal Conversion ratio
Returns:
Money if this CurrencyCode is the same as oTarget then this is returned without any modification, if if this CurrencyCode is different from oTarget then the returned value is this multiplied by oRatio.
Throws:
java.lang.NullPointerException - if oTarget is null

convertTo

public Money convertTo(java.lang.String sTarget,
                       java.math.BigDecimal oRatio)
                throws java.lang.NullPointerException,
                       java.lang.IllegalArgumentException

Convert this money to another currency

Parameters:
oTarget - Target CurrencyCode
oRatio - BigDecimal Conversion ratio
Returns:
Money if this CurrencyCode is the same as oTarget then this is returned without any modification, if if this CurrencyCode is different from oTarget then the returned value is this multiplied by oRatio.
Throws:
java.lang.NullPointerException - if oTarget is null
java.lang.IllegalArgumentException

convertTo

public Money convertTo(CurrencyCode oTarget)
                throws java.lang.NullPointerException,
                       java.lang.NumberFormatException

Convert this money to another currency using a web service for finding the conversion ratio

Parameters:
oTarget - Target CurrencyCode
Throws:
java.lang.NullPointerException - if oTarget is null
java.lang.NumberFormatException

convertTo

public Money convertTo(java.lang.String sTarget)
                throws java.lang.NullPointerException,
                       java.lang.NumberFormatException,
                       java.lang.IllegalArgumentException

Convert this money to another currency using a web service for finding the conversion ratio

Parameters:
sTarget - 3 letter Code of Target Currency
Throws:
java.lang.NullPointerException - if oTarget is null
java.lang.IllegalArgumentException - if sTarget is not a valid currency code
java.lang.NumberFormatException

format

public java.lang.String format(java.lang.String sLanguage,
                               java.lang.String sCountry)
Format a BigDecimal as a String following the rules for an specific language and country

Parameters:
sLanguage - String ISO-639 two letter language code
sCountry - String ISO-3166 two leter country code
Returns:
String

toString

public java.lang.String toString()
Overrides:
toString in class java.math.BigDecimal