Data Types | ||
ConversionType |
RoundMethod |
#include <rw/money/moneycalc.h> RWMoneyCalculator<double> calc(RWMoneyCalculator<double>::plain, 2u);
com.roguewave.money.currency.v1_0.MoneyCalculator
Class RWMoneyCalculator<T> can perform algebraic operations when the money operands in the equation are not of the same currency. Because some conversion from one currency to another must occur before such an operation can be carried out, RWMoneyCalculator<T> lets you specify a conversion policy for calculator objects.
Four conversion policies are available:
No Conversion. Any operations performed on monies of different currencies will throw an exception.
Base Currency Conversion. When arithmetic operations are performed on monies of different currencies both operands will first be converted to the base currency. The operation will then be performed and the result returned 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 and the result is returned 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 and the result is returned in the source currency.
Conversions are carried out through the RWExchangeFactory object that is associated with the particular calculator object.
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. Rounding a negative number is equivalent to rounding the absolute value of the negative number, then multiplying the result by negative one. Briefly, the available rounding policies available for RWMoneyCalculator are:
Don't round. No rounding will occur, and accuracy is ignored.
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.
enum ConversionType {noConversion, base, target, source}
Enumeration for the currency conversion policy.
enum RoundMethod {noRounding, up, down, plain, bankers}
Enumeration for rounding method.
RWMoneyCalculator();
Constructs a calculator object that does no rounding or currency conversion.
RWMoneyCalculator(const RWMoneyCalculator<T>&);
Copy constructor.
RWMoneyCalculator(RoundMethod rm, unsigned int accuracy, unsigned int roundDigit=5);
Construct a calculator that does rounding, but does not do currency conversion.
RWMoneyCalculator(const RWExchangeFactory<T>& ef, ConversionType ct, const RWCString& baseCurr="", RoundMethod rm=noRounding, unsigned int accuracy=2, unsigned int roundDigit=5);
Construct a calculator that does currency conversion and rounding, if other than the default RoundMethod is provided.
RWMoneyCalculator(const RWExchangeRateTable& ert, ConversionType ct, const RWCString& baseCurr="", RoundMethod rm=noRounding, unsigned int accuracy=2, unsigned int roundDigit=5);
Same as above, except use the given exchange rate table to construct the exchange factory argument.
RWMoneyCalculator<T>& operator=(const RWMoneyCalculator<T>& c);
Assignment operator.
RWMoney<T> abs(const RWMoney& money) const;
Returns the absolute value of the parameter.
unsigned int accuracy() const;
Returns the accuracy for this RWMoneyCalculator object.
RWMoney<T> add(const RWMoney<T>& opnd1, const RWMoney<T>& opnd2) const;
Adds the two RWMoney operands and returns the result.
Addition and subtraction are allowed on monies of different currency types if the conversion type for the calculator object is set to base, target, or source.
RWCString baseCurrency() const;
Returns the currency that is used when the conversion type is set to RWMoneyCalculator<T>::base.
RWMoneyCalculator<T>::ConversionType conversionType() const;
Returns the current conversion type for this RWMoneyCalculator object.
RWMoney<T> divide(const RWMoney<T>& opnd1, const T& opnd2) const;
Divides the first operand by the second and returns the result.
RWBoolean equal(const RWMoney<T>& lhs, RWMoney<T>& rhs)const;
Tests for equality for monies of the same or different currency types. If the currency types differ, the conversion type for the calculator object must be set to base, target, or source. This allows the values to be converted before they are compared.
RWExchangeFactory<T> exchangeFactory() const; RWExchangeFactory<T>& exchangeFactory();
Returns the exchange factory that is being used by the calculator. The non-const version may be used to modify the exchange factory.
RWBoolean greaterThan(const RWMoney<T>& lhs, RWMoney<T>& rhs)const; RWBoolean greaterThanOrEqual(const RWMoney<T>& lhs, RWMoney<T>& rhs)const;
Relational comparisons for monies of the same or different currency types. If the currency types differ, the conversion type for the calculator object must be set to base, target, or source. This allows the values to be converted before they are compared.
RWBoolean greaterThanZero(const RWMoney<T>& money)const;
Tests whether the indicated value is greater than zero.
RWBoolean isZero(const RWMoney<T>& money)const;
Tests whether the indicated value is equal to zero.
RWBoolean lessThan(const RWMoney<T>& lhs, RWMoney<T>& rhs)const; RWBoolean lessThanOrEqual(const RWMoney<T>& lhs, RWMoney<T>& rhs)const;
Relational comparisons for monies of the same or different currency types. If the currency types differ, the conversion type for the calculator object must be set to base, target, or source. This allows the values to be converted before they are compared.
RWBoolean lessThanZero(const RWMoney<T>& money)const;
Tests whether the indicated value is less than zero.
RWMoney<T> multiply(const RWMoney<T>& opnd1, const T& opnd2) const; RWMoney<T> multiply(const T& opnd1, const RWMoney<T>& opnd2) const;
Multiplies the first operand by the second and returns the result.
RWMoney<T> round(const RWMoney& money) const;
Rounds the provided RWMoney value according to the current rounding method and accuracy for RWMoneyCalculator.
unsigned int roundDigit() const;
Returns the current value for the round digit.
RWMoneyCalculator<T>::RoundMethod roundMethod() const;
Returns the rounding method for this RWMoneyCalculator object.
RWBoolean sameCurrency(const RWMoney<T>& opnd1,RWMoney<T>& opnd2)const;
Compares whether the given values have the same currency type.
void setaccuracy(unsigned int accuracy);
Sets the accuracy value for this RWMoneyCalculator object.
void setConversionType(RWMoneyCalculator<T>::ConversionType conversionType);
Sets the conversion type for this RWMoneyCalculator object.
void setExchangeFactory(const RWExchangeFactory<T>& f);
Sets the exchange factory used by the calculator.
void setRoundDigit(unsigned int roundDigit);
Sets the value for the rounding digit. The default value is 5.
void setRoundingMethod(RWMoneyCalculator<T>::RoundMethod method);
Sets the rounding method for the calculator.
RWMoney<T> subtract(const RWMoney<T>& opnd1, const RWMoney<T>& opnd2) const;
Subtracts the second operand from the first and returns the result.
Addition and subtraction are allowed on monies of different currency types if the conversion type for the calculator object is set to base, target, or source.
©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.