It is very possible that your applications will need to add money values of different currencies. Money.h++ provides C++ and Java money calculator classes that provide this functionality. In C++, the class is RWMoneyCalculator<T>. In Java, the class is MoneyCalculator.
Before you can use a money calculator class, you must consider and possibly set two policies. The first is the conversion policy, which determines what currency will be used for the result of a mixed-currency calculation. The second is the rounding policy, which determines how the money calculator should round decimal places.
Whenever your application performs arithmetic operations involving two or more different currencies, one or more currency conversions must take place. A conversion policy determines how the money calculator will express the result of its calculation.
There are four conversion policies available to the money calculator class:
No Conversion. An operations performed on monies of different currencies throws an exception.
Base Currency Conversion. When arithmetic operations are performed on monies of different currencies both operands are converted to a base currency. After performing the calculation the money calculator returns the result in the base currency.
Target Currency Conversion. When arithmetic operations are performed on monies of different currencies, the source operand (first argument) is converted to the target operand (second argument) currency. After performing the calculation, the money calculator returns the result in the target currency.
Source Currency Conversion. When arithmetic operations are performed on monies of different currencies, the target operand (second argument) is converted to the source operand (first argument) currency. After performing the calculation, the money calculator returns the result in the source currency.
A money calculator object's currency conversion type can be set at construction. It can also be set or changed using the setConversionType() member function.
In addition to setting the conversion type, you must also associate an RWExchangeFactory object with the calculator object. The RWExchangeFactory provides the exchange rate table that is used to perform the conversion. There's more on exchange rates and exchange rate tables in Section 5.2, "Exchange Rates and Exchange Rate Tables." The following C++ example shows how you construct an RWMoneyCalculator and some money objects, then perform mixed-currency calculations in C++:
typedef RWDecimal< RWMP2Int > Decimal; RWExchangeRateTable table; . . . RWExchangeFactory<Decimal> factory( table ); . . . RWMoneyCalculator<Decimal> calculator( factory, RWMoneyCalculator<Decimal>::target ); RWMoney<Decimal> lira(Decimal("12823"), "ITL" ); RWMoney<Decimal> marks(Decimal("1.26"), "DEM" ); RWMoney<Decimal> sum = calculator.add( lira, marks ); calculator.setConversionType( RWMoneyCalculator<Decimal>::base ); calculator.setBaseCurrency( "USD" ); sum = calculator.add( lira, marks ); . . .
The following Java example mirrors the operations in the C++ example.
// Create a new calculator and two money objects MoneyCalculator calculator = new MoneyCalculator(factory, MoneyCalculator.CONVERSION_TARGET); Money lira = new Money("12823", "ITL"); Money marks = new Money("1.26", "DEM"); // Sum is in marks Money sum = calculator.add(lira, marks); System.out.println(lira + " + " + marks + " = " + sum); calculator.setConversionPolicy(MoneyCalculator.CONVERSION_BASE); // Set the base currency to US Dollars calculator.setBaseCurrency("USD"); // Sum is in US Dollars sum = calculator.add(lira, marks);
In addition to setting the conversion policy, you must set a rounding policy and an accuracy for the calculator. The accuracy specifies how many digits to the right of the decimal points should be reported.
You can also set a comparison digit that lets you control when rounding up or down occurs. As described below, the plain and bankers rounding methods will always compare the digit to the right of the accuracy position to the comparison digit and increment the digit in the accuracy position, or leave it as is, based on the comparison. The default comparison digit is 5.
Rounding a negative number is equivalent to rounding the absolute value of the negative number, then multiplying the result by negative one.
The available rounding policies available for RWMoneyCalculator and MoneyCalculator are:
Round up. Always add one to the digit at the specified accuracy decimal place, then truncate any digits to the right.
Round down. Truncate all digits to the right of the specified accuracy decimal place.
Plain. If the digit one to the right of the specified accuracy decimal place is equal to or greater than a comparison digit, increase the digit at the accuracy position by one and truncate all numbers to the right. Otherwise, truncate all digits to the right of the accuracy place digit.
Bankers. If the digit one to the right of the specified accuracy decimal place is equal to a comparison digit, round so that the digit at the specified accuracy position is even. If the digit one to the right of the specified accuracy position is greater than a comparison digit, increment by one the digit at the specified accuracy position. If the digit one to the right of the specified accuracy position is less than a comparison digit, truncate all numbers to the right of the accuracy position.
Don't round. No rounding will occur, and accuracy is ignored.
Table 6 shows the effects of the rounding policies:
Method | 1.245 | 1.25 | 1.251 | 1.255 | 1.259 | -1.259 |
up accuracy = 2 |
1.25 |
1.25 |
1.26 |
1.26 |
1.26 |
-1.26 |
down accuracy = 2 |
1.24 |
1.25 |
1.25 |
1.25 |
1.25 |
-1.25 |
plain accuracy = 2 comparison = 5 |
1.25 |
1.25 |
1.25 |
1.26 |
1.26 |
-1.26 |
bankers accuracy = 2 comparison = 5 |
1.24 |
1.25 |
1.25 |
1.26 |
1.26 |
-1.26 |
noRounding |
1.245 |
1.25 |
1.251 |
1.255 |
1.259 |
-1.259 |
The default constructor for an RWMoneyCalculator, creates a money calculator without a rounding method or a currency conversion policy. For other constructors, the default rounding method is noRounding, the default accuracy is 2, and the default comparison digit is 5. RWMoneyCalculator provides member functions setAccuracy(), setRoundDigit(), and setRoundMethod() to set the accuracy, comparison digit and rounding method for the object.
Java's Money class uses the same rounding policy names, with the exception of noRounding which is Money.No_Round.
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.