แก้ไข

แชร์ผ่าน


CComCurrency Class

CComCurrency has methods and operators for creating and managing a CURRENCY object.

Syntax

class CComCurrency;

Members

Public constructors

Name Description
CComCurrency::CComCurrency The constructor for a CComCurrency object.

Public methods

Name Description
CComCurrency::GetCurrencyPtr Returns the address of an m_currency data member.
CComCurrency::GetFraction Call this method to return the fractional component of a CComCurrency object.
CComCurrency::GetInteger Call this method to return the integer component of a CComCurrency object.
CComCurrency::Round Call this method to round a CComCurrency object to the nearest integer value.
CComCurrency::SetFraction Call this method to set the fractional component of a CComCurrency object.
CComCurrency::SetInteger Call this method to set the integer component of a CComCurrency object.

Public operators

Name Description
CComCurrency::operator - This operator is used to perform subtraction on a CComCurrency object.
CComCurrency::operator != Compares two CComCurrency objects for inequality.
CComCurrency::operator * This operator is used to perform multiplication on a CComCurrency object.
CComCurrency::operator *= This operator is used to perform multiplication on a CComCurrency object and assign it the result.
CComCurrency::operator / This operator is used to perform division on a CComCurrency object.
CComCurrency::operator /= This operator is used to perform division on a CComCurrency object and assign it the result.
CComCurrency::operator + This operator is used to perform addition on a CComCurrency object.
CComCurrency::operator += This operator is used to perform addition on a CComCurrency object and assign the result to the current object.
CComCurrency::operator < This operator compares two CComCurrency objects to determine the lesser.
CComCurrency::operator <= This operator compares two CComCurrency objects to determine equality or the lesser.
CComCurrency::operator = This operator assigns the CComCurrency object to a new value.
CComCurrency::operator -= This operator is used to perform subtraction on a CComCurrency object and assign it the result.
CComCurrency::operator == This operator compares two CComCurrency objects for equality.
CComCurrency::operator > This operator compares two CComCurrency objects to determine the larger.
CComCurrency::operator >= This operator compares two CComCurrency objects to determine equality or the larger.
CComCurrency::operator CURRENCY Casts a CURRENCY object.

Public data members

Name Description
CComCurrency::m_currency The CURRENCY variable created by your class instance.

Remarks

CComCurrency is a wrapper for the CURRENCY data type. CURRENCY is implemented as an 8-byte two's-complement integer value scaled by 10,000. This scaling gives a fixed-point number with 15 digits left of the decimal point and 4 digits to the right. The CURRENCY data type is useful for calculations involving money, or for any fixed-point calculations where accuracy is important.

The CComCurrency wrapper implements arithmetic, assignment, and comparison operations for this fixed-point type. The supported applications have been selected to control the rounding errors that can occur during fixed-point calculations.

The CComCurrency object provides access to the numbers on either side of the decimal point in the form of two components: an integer component, which stores the value to the left of the decimal point, and a fractional component, which stores the value to the right of the decimal point. The fractional component is stored internally as an integer value between -9999 (CY_MIN_FRACTION) and +9999 (CY_MAX_FRACTION). The method CComCurrency::GetFraction returns a value scaled by a factor of 10000 (CY_SCALE).

When specifying the integer and fractional components of a CComCurrency object, remember that the fractional component is a number in the range 0 to 9999. This consideration is important when dealing with a currency such as the US dollar. Dollar amounts are commonly expressed using only two significant digits after the decimal point. Even though the last two digits aren't displayed, they must be taken into account.

Value Possible CComCurrency assignments
$10.50 CComCurrency(10,5000) or CComCurrency(10.50)
$10.05 CComCurrency(10,500) or CComCurrency(10.05)

The values CY_MIN_FRACTION, CY_MAX_FRACTION, and CY_SCALE are defined in atlcur.h.

Requirements

Header: atlcur.h

CComCurrency::CComCurrency

The constructor.

CComCurrency() throw();
CComCurrency(const CComCurrency& curSrc) throw();
CComCurrency(CURRENCY cySrc) throw();
CComCurrency(DECIMAL dSrc);
CComCurrency(ULONG ulSrc);
CComCurrency(USHORT usSrc);
CComCurrency(CHAR cSrc);
CComCurrency(DOUBLE dSrc);
CComCurrency(FLOAT fSrc);
CComCurrency(LONG lSrc);
CComCurrency(SHORT sSrc);
CComCurrency(BYTE bSrc);
CComCurrency(LONGLONG nInteger, SHORT nFraction);
explicit CComCurrency(LPDISPATCH pDispSrc);
explicit CComCurrency(const VARIANT& varSrc);
explicit CComCurrency(LPCWSTR szSrc);
explicit CComCurrency(LPCSTR szSrc);

Parameters

curSrc
An existing CComCurrency object.

cySrc
A variable of type CURRENCY.

bSrc, dSrc, fSrc, lSrc, sSrc, ulSrc, usSrc
The initial value given to the member variable m_currency.

cSrc
A character containing the initial value given to the member variable m_currency.

nInteger, nFraction
The initial monetary value's integer and fractional components. For more information, see the CComCurrency overview.

pDispSrc
An IDispatch pointer.

varSrc
A variable of type VARIANT. The locale of the current thread is used to perform the conversion.

szSrc
A Unicode or ANSI string containing the initial value. The locale of the current thread is used to perform the conversion.

Remarks

The constructor sets the initial value of CComCurrency::m_currency, and accepts a wide range of data types, including integers, strings, floating-point numbers, CURRENCY variables, and other CComCurrency objects. If no value is provided, m_currency is set to 0.

If there's an error, such as an overflow, the constructors lacking an empty exception specification (throw()) call AtlThrow with an HRESULT describing the error.

When using floating-point or double values to assign a value, remember that CComCurrency(10.50) is equivalent to CComCurrency(10,5000), and not CComCurrency(10,50).

CComCurrency::GetCurrencyPtr

Returns the address of an m_currency data member.

CURRENCY* GetCurrencyPtr() throw();

Return value

Returns the address of an m_currency data member

CComCurrency::GetFraction

Call this method to return the fractional component of the CComCurrency object.

SHORT GetFraction() const;

Return value

Returns the fractional component of the m_currency data member.

Remarks

The fractional component is a 4-digit integer value between -9999 (CY_MIN_FRACTION) and +9999 (CY_MAX_FRACTION). GetFraction returns this value scaled by 10000 (CY_SCALE). The values of CY_MIN_FRACTION, CY_MAX_FRACTION, and CY_SCALE are defined in atlcur.h.

Example

CComCurrency cur(10, 5000);
int nFract;
nFract = cur.GetFraction();
ATLASSERT(nFract == 5000);   

CComCurrency::GetInteger

Call this method to get the integer component of a CComCurrency object.

LONGLONG GetInteger() const;

Return value

Returns the integer component of the m_currency data member.

Example

CComCurrency cur(10, 5000);
LONGLONG nInteger;
nInteger = cur.GetInteger();
ATLASSERT(nInteger == 10);   

CComCurrency::m_currency

The CURRENCY data member.

CURRENCY m_currency;

Remarks

This member holds the currency accessed and manipulated by the methods of this class.

CComCurrency::operator -

This operator is used to perform subtraction on a CComCurrency object.

CComCurrency operator-() const;
CComCurrency operator-(const CComCurrency& cur) const;

Parameters

cur
A CComCurrency object.

Return value

Returns a CComCurrency object representing the result of the subtraction. If there's an error, such as an overflow, this operator calls AtlThrow with an HRESULT describing the error.

Example

CComCurrency cur1(10, 5000), cur2;
cur2 = cur1 - CComCurrency(4, 5000);
ATLASSERT(cur2 == CComCurrency(6, 0));   

CComCurrency::operator !=

This operator compares two objects for inequality.

bool operator!= (const CComCurrency& cur) const;

Parameters

cur
The CComCurrency object to be compared.

Return value

Returns TRUE if the item being compared isn't equal to the CComCurrency object; otherwise, FALSE.

Example

CComCurrency cur1(10, 5000), cur2(10, 5001);
ATLASSERT(cur1 != cur2);   

CComCurrency::operator *

This operator is used to perform multiplication on a CComCurrency object.

CComCurrency operator*(long nOperand) const;
CComCurrency operator*(const CComCurrency& cur) const;

Parameters

nOperand
The multiplier.

cur
The CComCurrency object used as the multiplier.

Return value

Returns a CComCurrency object representing the result of the multiplication. If there's an error, such as an overflow, this operator calls AtlThrow with an HRESULT describing the error.

Example

CComCurrency cur1(10, 5000), cur2;
cur2 = cur1 * 2;
ATLASSERT(cur2 == CComCurrency(21, 0));   

CComCurrency::operator *=

This operator is used to perform multiplication on a CComCurrency object and assign it the result.

const CComCurrency& operator*= (long nOperand);
const CComCurrency& operator*= (const CComCurrency& cur);

Parameters

nOperand
The multiplier.

cur
The CComCurrency object used as the multiplier.

Return value

Returns the updated CComCurrency object. If there's an error, such as an overflow, this operator calls AtlThrow with an HRESULT describing the error.

Example

CComCurrency cur(10, 5000);
cur *= 2;
ATLASSERT(cur == CComCurrency(21, 0));   

CComCurrency::operator /

This operator is used to perform division on a CComCurrency object.

CComCurrency operator/(long nOperand) const;

Parameters

nOperand
The divisor.

Return value

Returns a CComCurrency object representing the result of the division. If the divisor is 0, an assert failure will occur.

Example

CComCurrency cur1(10, 5000), cur2;
cur2 = cur1 / 10;
ATLASSERT(cur2 == CComCurrency(1, 500));

CComCurrency::operator /=

This operator is used to perform division on a CComCurrency object and assign it the result.

const CComCurrency& operator/= (long nOperand);

Parameters

nOperand
The divisor.

Return value

Returns the updated CComCurrency object. If the divisor is 0, an assert failure will occur.

Example

CComCurrency cur(10, 5000);
cur /= 10;
ATLASSERT(cur == CComCurrency(1, 500));

CComCurrency::operator +

This operator is used to perform addition on a CComCurrency object.

CComCurrency operator+(const CComCurrency& cur) const;

Parameters

cur
The CComCurrency object to be added to the original object.

Return value

Returns a CComCurrency object representing the result of the addition. If there's an error, such as an overflow, this operator calls AtlThrow with an HRESULT describing the error.

Example

CComCurrency cur1(10, 5000), cur2;
cur2 = cur1 + CComCurrency(4, 5000);
ATLASSERT(cur2 == CComCurrency(15, 0));

CComCurrency::operator +=

This operator is used to perform addition on a CComCurrency object and assign the result to the current object.

const CComCurrency& operator+= (const CComCurrency& cur);

Parameters

cur
The CComCurrency object.

Return value

Returns the updated CComCurrency object. If there's an error, such as an overflow, this operator calls AtlThrow with an HRESULT describing the error.

Example

CComCurrency cur(10, 2500);
cur += CComCurrency(4, 2500);
ATLASSERT(cur == CComCurrency(14, 5000));

CComCurrency::operator <

This operator compares two CComCurrency objects to determine the lesser.

bool operator<(const CComCurrency& cur) const;

Parameters

cur
A CComCurrency object.

Return value

Returns TRUE if the first object is less than the second, FALSE otherwise.

Example

CComCurrency cur1(10, 4900);
CComCurrency cur2(10, 5000);
ATLASSERT(cur1 < cur2);

CComCurrency::operator <=

This operator compares two CComCurrency objects to determine equality or the lesser.

bool operator<= (const CComCurrency& cur) const;

Parameters

cur
A CComCurrency object.

Return value

Returns TRUE if the first object is less than or equal to the second, FALSE otherwise.

Example

CComCurrency cur1(10, 4900);
CComCurrency cur2(10, 5000);
ATLASSERT(cur1 <= cur2);

CComCurrency::operator =

This operator assigns the CComCurrency object to a new value.

const CComCurrency& operator= (const CComCurrency& curSrc) throw();
const CComCurrency& operator= (CURRENCY cySrc) throw();
const CComCurrency& operator= (FLOAT fSrc);
const CComCurrency& operator= (SHORT sSrc);
const CComCurrency& operator= (LONG lSrc);
const CComCurrency& operator= (BYTE bSrc);
const CComCurrency& operator= (USHORT usSrc);
const CComCurrency& operator= (DOUBLE dSrc);
const CComCurrency& operator= (CHAR cSrc);
const CComCurrency& operator= (ULONG ulSrc);
const CComCurrency& operator= (DECIMAL dSrc);

Parameters

curSrc
A CComCurrency object.

cySrc
A variable of type CURRENCY.

sSrc, fSrc, lSrc, bSrc, usSrc, dSrc, cSrc, ulSrc, dSrc
The numeric value to assign to the CComCurrency object.

Return value

Returns the updated CComCurrency object. If there's an error, such as an overflow, this operator calls AtlThrow with an HRESULT describing the error.

Example

CComCurrency cur1, cur2(10, 5000);
CURRENCY cy;

// Copying one object to another 
cur1 = cur2;

// Using the CURRENCY data type
cy.int64 = 105000;
cur1 = cy;

ATLASSERT(cur1 == cur2);

CComCurrency::operator -=

This operator is used to perform subtraction on a CComCurrency object and assign it the result.

const CComCurrency& operator-= (const CComCurrency& cur);

Parameters

cur
A CComCurrency object.

Return value

Returns the updated CComCurrency object. If there's an error, such as an overflow, this operator calls AtlThrow with an HRESULT describing the error.

Example

CComCurrency cur(10, 5000);
cur -= CComCurrency(4, 5000);
ATLASSERT(cur == CComCurrency(6, 0));

CComCurrency::operator ==

This operator compares two CComCurrency objects for equality.

bool operator== (const CComCurrency& cur) const;

Parameters

cur
The CComCurrency object to compare.

Return value

Returns TRUE if the objects are equal (that is, the m_currency data members, both integer and fractional, in both objects have the same value), FALSE otherwise.

Example

CComCurrency cur1(10, 5000), cur2;
cur2 = cur1;
ATLASSERT(cur1 == cur2);

CComCurrency::operator >

This operator compares two CComCurrency objects to determine the larger.

bool operator>(const CComCurrency& cur) const;

Parameters

cur
A CComCurrency object.

Return value

Returns TRUE if the first object is greater than the second, FALSE otherwise.

Example

CComCurrency cur1(10, 5100);
CComCurrency cur2(10, 5000);
ATLASSERT(cur1 > cur2);

CComCurrency::operator >=

This operator compares two CComCurrency objects to determine equality or the larger.

bool operator>= (const CComCurrency& cur) const;

Parameters

cur
A CComCurrency object.

Return value

Returns TRUE if the first object is greater than or equal to the second, FALSE otherwise.

Example

CComCurrency cur1(10, 5100);
CComCurrency cur2(10, 5000);
ATLASSERT(cur1 >= cur2);

CComCurrency::operator CURRENCY

These operators are used to cast a CComCurrency object to a CURRENCY data type.

operator CURRENCY&() throw();
operator const CURRENCY&() const throw();

Return value

Returns a reference to a CURRENCY object.

Example

CComCurrency cur(10, 5000);
CURRENCY cy = static_cast<CURRENCY>(cur); // Note that explicit cast is not necessary
ATLASSERT(cy.int64 == 105000);

CComCurrency::Round

Call this method to round the currency to a specified number of decimal places.

HRESULT Roundint nDecimals);

Parameters

nDecimals
The number of digits to which m_currency will be rounded, in the range 0 to 4.

Return value

Returns S_OK on success, or an error HRESULT on failure.

Example

CComCurrency cur(10, 1234);
cur.Round(3);
ATLASSERT(cur.GetFraction() == 1230);   

CComCurrency::SetFraction

Call this method to set the fractional component of a CComCurrency object.

HRESULT SetFraction(SHORT nFraction);

Parameters

nFraction
The value to assign to the fractional component of the m_currency data member. The sign of the fractional component must be the same as the integer component, and the value must be in the range -9999 (CY_MIN_FRACTION) to +9999 (CY_MAX_FRACTION).

Return value

Returns S_OK on success, or an error HRESULT on failure.

Example

CComCurrency cur(10, 0);
cur.SetFraction(5000);
ATLASSERT(CComCurrency(10, 5000) == cur);   

CComCurrency::SetInteger

Call this method to set the integer component of a CComCurrency object.

HRESULT SetInteger(LONGLONG nInteger);

Parameters

nInteger
The value to be assigned to the integer component of the m_currency data member. The sign of the integer component must match the sign of the existing fractional component.

nInteger must be in the range CY_MIN_INTEGER to CY_MAX_INTEGER, inclusive. These values are defined in atlcur.h.

Return value

Returns S_OK on success, or an error HRESULT on failure.

Example

CComCurrency cur(0, 5000);
cur.SetInteger(10);
ATLASSERT(CComCurrency(10, 5000) == cur);   

See also

COleCurrency class
CURRENCY
Class overview