CComCurrency
类
CComCurrency
具有用于创建和管理 CURRENCY
对象的方法和运算符。
语法
class CComCurrency;
成员
公共构造函数
名称 | 描述 |
---|---|
CComCurrency::CComCurrency |
CComCurrency 对象的构造函数。 |
公共方法
名称 | 描述 |
---|---|
CComCurrency::GetCurrencyPtr |
返回 m_currency 数据成员的地址。 |
CComCurrency::GetFraction |
调用此方法以返回 CComCurrency 对象的小数部分。 |
CComCurrency::GetInteger |
调用此方法以返回 CComCurrency 对象的整数部分。 |
CComCurrency::Round |
调用此方法以将 CComCurrency 对象舍入为最接近的整数值。 |
CComCurrency::SetFraction |
调用此方法以设置 CComCurrency 对象的小数部分。 |
CComCurrency::SetInteger |
调用此方法以设置 CComCurrency 对象的整数部分。 |
公共运算符
“属性” | 描述 |
---|---|
CComCurrency::operator - |
此运算符用于对 CComCurrency 对象执行减法。 |
CComCurrency::operator != |
比较两个 CComCurrency 对象是否相等。 |
CComCurrency::operator * |
此运算符用于对 CComCurrency 对象执行乘法。 |
CComCurrency::operator *= |
此运算符用于对 CComCurrency 对象执行乘法并对它赋予结果。 |
CComCurrency::operator / |
此运算符用于对 CComCurrency 对象执行除法。 |
CComCurrency::operator /= |
此运算符用于对 CComCurrency 对象执行除法并对它赋予结果。 |
CComCurrency::operator + |
此运算符用于对 CComCurrency 对象执行加法。 |
CComCurrency::operator += |
此运算符用于对 CComCurrency 对象执行加法并对它赋予结果。 |
CComCurrency::operator < |
此运算符比较两个 CComCurrency 对象以确定较小者。 |
CComCurrency::operator <= |
此运算符比较两个 CComCurrency 对象以确定是否相等或较小者。 |
CComCurrency::operator = |
此运算符向 CComCurrency 对象赋予新值。 |
CComCurrency::operator -= |
此运算符用于对 CComCurrency 对象执行减法并对它赋予结果。 |
CComCurrency::operator == |
此运算符比较两个 CComCurrency 对象是否相等。 |
CComCurrency::operator > |
此运算符比较两个 CComCurrency 对象以确定较大者。 |
CComCurrency::operator >= |
此运算符比较两个 CComCurrency 对象以确定是否相等或较大者。 |
CComCurrency::operator CURRENCY |
强制转换 CURRENCY 对象。 |
公共数据成员
“属性” | 描述 |
---|---|
CComCurrency::m_currency |
由类实例创建的 CURRENCY 变量。 |
备注
CComCurrency
是 CURRENCY
数据类型的包装器。 CURRENCY
是作为按 10,000 缩放的 8 字节补码整数值实现的。 此缩放产生了一个定点数,小数点左侧有 15 位数,右侧有 4 位数。 CURRENCY
数据类型对于涉及到货币的计算或精确度至关重要的所有定点计算很有用。
CComCurrency
包装器实现此定点类型的算术、赋值和比较操作。 已选中受支持的应用程序,以控制定点计算过程中可能出现的舍入误差。
CComCurrency
对象以两个组件的形式提供对小数点任意一侧的数字的访问权限:一个整数组件,存储小数点左侧的值;一个小数组件,存储小数点右侧的值。 小数部分作为 -9999 (CY_MIN_FRACTION
) 到 +9999 (CY_MAX_FRACTION
) 之间的整数值存储在内部。 方法 CComCurrency::GetFraction
返回一个按 10000 (CY_SCALE
) 倍缩放的值。
指定 CComCurrency
对象的整数和小数部分时,请记住小数部分是 0 到 9999 范围内的数字。 在处理美元之类的货币时,此考量很有用。 美元金额通常以小数点后的两个有效位数来表示。 即使不显示最后两位数字,也必须将它们考虑在内。
值 | 可能的 CComCurrency 赋值 |
---|---|
$10.50 | CComCurrency(10,5000) 或 CComCurrency(10.50) |
$10.05 | CComCurrency(10,500) 或 CComCurrency(10.05) |
CY_MIN_FRACTION
、CY_MAX_FRACTION
和 CY_SCALE
的值在 atlcur.h 中定义。
要求
头文件:atlcur.h
CComCurrency::CComCurrency
构造函数。
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);
参数
curSrc
一个现有的 CComCurrency
对象。
cySrc
CURRENCY
类型的变量。
bSrc
, dSrc
, fSrc
, lSrc
, sSrc
, ulSrc
, usSrc
提供给成员变量 m_currency
的初始值。
cSrc
一个字符,包含提供给成员变量 m_currency
的初始值。
nInteger
,nFraction
初始货币值的整数和小数部分。 有关详细信息,请参阅 CComCurrency
概述。
pDispSrc
IDispatch
指针。
varSrc
VARIANT
类型的变量。 当前线程的区域设置用于执行转换。
szSrc
包含初始值的 Unicode 或 ANSI 字符串。 当前线程的区域设置用于执行转换。
注解
构造函数设置初始值 CComCurrency::m_currency
,并接受各种数据类型,其中包括整数、字符串、浮点数、CURRENCY
变量和其他 CComCurrency
对象。 如果不提供任何值,则会将 m_currency
设置为 0。
如果出现错误(如溢出),则缺少空异常规范 (throw()
) 的构造函数会调用 AtlThrow
并使用 HRESULT
来描述错误。
使用浮点值或双精度值来赋值时,请记住 CComCurrency(10.50)
等效于 CComCurrency(10,5000)
而不是 CComCurrency(10,50)
。
CComCurrency::GetCurrencyPtr
返回 m_currency
数据成员的地址。
CURRENCY* GetCurrencyPtr() throw();
返回值
返回 m_currency
数据成员的地址
CComCurrency::GetFraction
调用此方法以返回 CComCurrency
对象的小数部分。
SHORT GetFraction() const;
返回值
返回 m_currency
数据成员的小数部分。
备注
小数部分是 -9999 (CY_MIN_FRACTION
) 到 +9999 (CY_MAX_FRACTION
) 之间的 4 位整数值。 GetFraction
返回的此值以 10000 (CY_SCALE
) 为倍数进行了缩放。 CY_MIN_FRACTION
、CY_MAX_FRACTION
和 CY_SCALE
的值在 atlcur.h 中定义。
示例
CComCurrency cur(10, 5000);
int nFract;
nFract = cur.GetFraction();
ATLASSERT(nFract == 5000);
CComCurrency::GetInteger
调用此方法以获取 CComCurrency
对象的整数部分。
LONGLONG GetInteger() const;
返回值
返回 m_currency
数据成员的整数部分。
示例
CComCurrency cur(10, 5000);
LONGLONG nInteger;
nInteger = cur.GetInteger();
ATLASSERT(nInteger == 10);
CComCurrency::m_currency
CURRENCY
数据成员。
CURRENCY m_currency;
注解
此成员保存由此类的方法访问和操作的货币。
CComCurrency::operator -
此运算符用于对 CComCurrency
对象执行减法。
CComCurrency operator-() const;
CComCurrency operator-(const CComCurrency& cur) const;
参数
cur
CComCurrency
对象。
返回值
返回一个表示减法结果的 CComCurrency
对象。 如果出现错误(如溢出),则此运算符会调用 AtlThrow
,使用 HRESULT
来描述错误。
示例
CComCurrency cur1(10, 5000), cur2;
cur2 = cur1 - CComCurrency(4, 5000);
ATLASSERT(cur2 == CComCurrency(6, 0));
CComCurrency::operator !=
此运算符比较两个 对象是否不相等。
bool operator!= (const CComCurrency& cur) const;
参数
cur
要比较的 CComCurrency
对象。
返回值
如果所比较的项不等于 CComCurrency
对象,则返回 TRUE
;否则返回 FALSE
。
示例
CComCurrency cur1(10, 5000), cur2(10, 5001);
ATLASSERT(cur1 != cur2);
CComCurrency::operator *
此运算符用于对 CComCurrency
对象执行乘法。
CComCurrency operator*(long nOperand) const;
CComCurrency operator*(const CComCurrency& cur) const;
参数
nOperand
乘数。
cur
用作乘数的 CComCurrency
对象。
返回值
返回一个表示乘法结果的 CComCurrency
对象。 如果出现错误(如溢出),则此运算符会调用 AtlThrow
,使用 HRESULT
来描述错误。
示例
CComCurrency cur1(10, 5000), cur2;
cur2 = cur1 * 2;
ATLASSERT(cur2 == CComCurrency(21, 0));
CComCurrency::operator *=
此运算符用于对 CComCurrency
对象执行乘法并对它赋予结果。
const CComCurrency& operator*= (long nOperand);
const CComCurrency& operator*= (const CComCurrency& cur);
参数
nOperand
乘数。
cur
用作乘数的 CComCurrency
对象。
返回值
返回更新的 CComCurrency
对象。 如果出现错误(如溢出),则此运算符会调用 AtlThrow
,使用 HRESULT
来描述错误。
示例
CComCurrency cur(10, 5000);
cur *= 2;
ATLASSERT(cur == CComCurrency(21, 0));
CComCurrency::operator /
此运算符用于对 CComCurrency
对象执行除法。
CComCurrency operator/(long nOperand) const;
参数
nOperand
除数。
返回值
返回一个表示除法结果的 CComCurrency
对象。 如果除数为 0,则会发生断言失败。
示例
CComCurrency cur1(10, 5000), cur2;
cur2 = cur1 / 10;
ATLASSERT(cur2 == CComCurrency(1, 500));
CComCurrency::operator /=
此运算符用于对 CComCurrency
对象执行除法并对它赋予结果。
const CComCurrency& operator/= (long nOperand);
参数
nOperand
除数。
返回值
返回更新的 CComCurrency
对象。 如果除数为 0,则会发生断言失败。
示例
CComCurrency cur(10, 5000);
cur /= 10;
ATLASSERT(cur == CComCurrency(1, 500));
CComCurrency::operator +
此运算符用于对 CComCurrency
对象执行加法。
CComCurrency operator+(const CComCurrency& cur) const;
参数
cur
要添加到原始对象的 CComCurrency
对象。
返回值
返回一个表示加法结果的 CComCurrency
对象。 如果出现错误(如溢出),则此运算符会调用 AtlThrow
,使用 HRESULT
来描述错误。
示例
CComCurrency cur1(10, 5000), cur2;
cur2 = cur1 + CComCurrency(4, 5000);
ATLASSERT(cur2 == CComCurrency(15, 0));
CComCurrency::operator +=
此运算符用于对 CComCurrency
对象执行加法并对它赋予结果。
const CComCurrency& operator+= (const CComCurrency& cur);
参数
cur
CComCurrency
对象。
返回值
返回更新的 CComCurrency
对象。 如果出现错误(如溢出),则此运算符会调用 AtlThrow
,使用 HRESULT
来描述错误。
示例
CComCurrency cur(10, 2500);
cur += CComCurrency(4, 2500);
ATLASSERT(cur == CComCurrency(14, 5000));
CComCurrency::operator <
此运算符比较两个 CComCurrency
对象以确定较小者。
bool operator<(const CComCurrency& cur) const;
参数
cur
CComCurrency
对象。
返回值
如果第一个对象小于第二个对象,则返回 TRUE
,否则返回 FALSE
。
示例
CComCurrency cur1(10, 4900);
CComCurrency cur2(10, 5000);
ATLASSERT(cur1 < cur2);
CComCurrency::operator <=
此运算符比较两个 CComCurrency
对象以确定是否相等或较小者。
bool operator<= (const CComCurrency& cur) const;
参数
cur
CComCurrency
对象。
返回值
如果第一个对象小于或等于第二个对象,则返回 TRUE
,否则返回 FALSE
。
示例
CComCurrency cur1(10, 4900);
CComCurrency cur2(10, 5000);
ATLASSERT(cur1 <= cur2);
CComCurrency::operator =
此运算符向 CComCurrency
对象赋予新值。
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);
参数
curSrc
CComCurrency
对象。
cySrc
CURRENCY
类型的变量。
sSrc
、fSrc
、lSrc
、bSrc
、usSrc
、dSrc
、cSrc
、ulSrc
、dSrc
要分配给 CComCurrency
对象的数值。
返回值
返回更新的 CComCurrency
对象。 如果出现错误(如溢出),则此运算符会调用 AtlThrow
,使用 HRESULT
来描述错误。
示例
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 -=
此运算符用于对 CComCurrency
对象执行减法并对它赋予结果。
const CComCurrency& operator-= (const CComCurrency& cur);
参数
cur
CComCurrency
对象。
返回值
返回更新的 CComCurrency
对象。 如果出现错误(如溢出),则此运算符会调用 AtlThrow
,使用 HRESULT
来描述错误。
示例
CComCurrency cur(10, 5000);
cur -= CComCurrency(4, 5000);
ATLASSERT(cur == CComCurrency(6, 0));
CComCurrency::operator ==
此运算符比较两个 CComCurrency
对象是否相等。
bool operator== (const CComCurrency& cur) const;
参数
cur
要比较的 CComCurrency
对象。
返回值
如果对象相等(即两个对象中的 m_currency
数据成员(整数和小数部分)具有相同的值),则返回 TRUE
,否则返回 FALSE
。
示例
CComCurrency cur1(10, 5000), cur2;
cur2 = cur1;
ATLASSERT(cur1 == cur2);
CComCurrency::operator >
此运算符比较两个 CComCurrency
对象以确定较大者。
bool operator>(const CComCurrency& cur) const;
参数
cur
CComCurrency
对象。
返回值
如果第一个对象大于第二个对象,则返回 TRUE
,否则返回 FALSE
。
示例
CComCurrency cur1(10, 5100);
CComCurrency cur2(10, 5000);
ATLASSERT(cur1 > cur2);
CComCurrency::operator >=
此运算符比较两个 CComCurrency
对象以确定是否相等或较大者。
bool operator>= (const CComCurrency& cur) const;
参数
cur
CComCurrency
对象。
返回值
如果第一个对象大于或等于第二个对象,则返回 TRUE
,否则返回 FALSE
。
示例
CComCurrency cur1(10, 5100);
CComCurrency cur2(10, 5000);
ATLASSERT(cur1 >= cur2);
CComCurrency::operator CURRENCY
这些运算符用于将 CComCurrency
对象强制转换为 CURRENCY
数据类型。
operator CURRENCY&() throw();
operator const CURRENCY&() const throw();
返回值
返回对 CURRENCY
对象的引用。
示例
CComCurrency cur(10, 5000);
CURRENCY cy = static_cast<CURRENCY>(cur); // Note that explicit cast is not necessary
ATLASSERT(cy.int64 == 105000);
CComCurrency::Round
调用此方法将货币舍入为指定的小数位数。
HRESULT Roundint nDecimals);
参数
nDecimals
在 0 到 4 的范围内将 m_currency
舍入的位数。
返回值
成功时返回 S_OK
,失败时返回错误 HRESULT
。
示例
CComCurrency cur(10, 1234);
cur.Round(3);
ATLASSERT(cur.GetFraction() == 1230);
CComCurrency::SetFraction
调用此方法以设置 CComCurrency
对象的小数部分。
HRESULT SetFraction(SHORT nFraction);
参数
nFraction
要分配给 m_currency
数据成员的小数部分的值。 小数部分的符号必须与整数部分的相同,值必须在 -9999 (CY_MIN_FRACTION
) 到 +9999 (CY_MAX_FRACTION
) 的范围内。
返回值
成功时返回 S_OK
,失败时返回错误 HRESULT
。
示例
CComCurrency cur(10, 0);
cur.SetFraction(5000);
ATLASSERT(CComCurrency(10, 5000) == cur);
CComCurrency::SetInteger
调用此方法以设置 CComCurrency
对象的整数部分。
HRESULT SetInteger(LONGLONG nInteger);
参数
nInteger
要分配给 m_currency
数据成员的整数部分的值。 整数部分的符号必须与现有小数部分的符号匹配。
nInteger
必须在 CY_MIN_INTEGER
到 CY_MAX_INTEGER
(含)的范围内。 这些值在 atlcur.h 中定义。
返回值
成功时返回 S_OK
,失败时返回错误 HRESULT
。
示例
CComCurrency cur(0, 5000);
cur.SetInteger(10);
ATLASSERT(CComCurrency(10, 5000) == cur);
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈