Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Currency Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

4.4 Mixed-Currency Calculations

It is very possible that your applications will need to add money values of different currencies. The Currency Module provides a money calculator class in the class RWMoneyCalculator<T>.

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. For more information on exchange rates and exchange rate tables, see Section 5.2, "Exchange Rates and Exchange Rate Tables." The following example shows how you construct an RWMoneyCalculator<T> and some money objects, then perform mixed-currency calculations:

4.4.2 Setting the Precision and Rounding Method

In addition to setting the conversion policy, you must set a rounding policy and a precision for the calculator. The precision 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. The comparison digit works only with plain or bankers rounding. As described below, the plain and bankers rounding methods always compare the digit to the right of the precision position to the comparison digit and then increment the digit in the precision 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 rounding policies available for RWMoneyCalculator are:

Table 3 shows the effects of the rounding policies:

Table 3: Methods of rounding

Method 1.245 1.25 1.251 1.255 1.259 -1.259 1.26500 1.26501
UP
precision = 2
1.25 1.25 1.26 1.26 1.26 -1.26 1.27 1.27
DOWN
precision = 2
1.24 1.25 1.25 1.25 1.25 -1.25 1.26 1.26
PLAIN
precision = 2
comparison = 5
1.24 1.25 1.25 1.25 1.26 -1.26 1.26 1.26
BANKERS
precision = 2
comparison = 5
1.24 1.25 1.25 1.26 1.26 -1.26 1.26 1.27
NO ROUNDING 1.245 1.25 1.251 1.255 1.259 -1.259 1.26500 1.26501

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 precision is 2, and the default comparison digit is 5. RWMoneyCalculator provides member functions setAccuracy(), setRoundDigit(), and setRoundMethod() to set the precision, comparison digit, and rounding method for the object.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.