SourcePro Analysis : Currency Module User’s Guide : Currency Conversions : Exchange Rates and Exchange Rate Tables
Exchange Rates and Exchange Rate Tables
Most currency exchange rates are published in the form:
 
1 US Dollar = 1.5455 Canadian Dollars
In the Currency Module parlance, 1.5455 is a multiplicative conversion factor for converting US Dollars to Canadian Dollars. This means that you multiply an amount in US Dollars by 1.5455 to convert to Canadian Dollars. The class RWExchangeRate encapsulates the information shown above by storing a source currency, a target currency and, by convention, a multiplicative conversion factor. The following example shows how you initialize RWExchangeRate object usToCan with the factor shown above:
 
#include <rw/currency/exchgrate.h>
 
RWExchangeRate usToCan("USD", "CAD", 1.5455);
RWExchangeRate provides members that can return or set an object’s source currency mnemonic, target currency mnemonic or rate. It also provides functions and operators that can persist an object to, and restore an object from a virtual stream or a file.
The class RWExchangeRateTable stores exchange rates as unique pairs of source and target currencies and an associated rate. No two exchange rates in the table have the same source/target currency pair.
The RWExchangeRateTable class can be initialized from a stream, in particular, from an ifstream (file), using the initialize() member function. The format for the stream is:
 
BEGIN_EXCHANGE
source=###
target=###
rate=###
END_EXCHANGE
For example, the following code initializes exchange rate table rates from the file exchange_rates.txt.
 
#include<rw/currency/exchgratetbl.h>
ifstream strm("exchange_rates.txt");
 
RWExchangeRateTable rates;
 
rates.initialize(strm);
An RWExchangeRateTable, when combined with one or more currency exchange groups, provides the basis for an exchange factory that can be used to convert money from one currency to another. Currency Exchange Factory shows how to set up a factory.