Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

4.4 Mixed-Currency Calculations

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.

4.4.1 Setting the Conversion Policy

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:

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++:

The following Java example mirrors the operations in the C++ example.

4.4.2 Setting the Accuracy and Rounding Method

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:

Table 6 shows the effects of the rounding policies:

Table 6 -- Methods of rounding

Method1.2451.251.2511.2551.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.


Previous fileTop of DocumentContentsIndexNext file

©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.