随便写一个将数字转换成大写金额的程序片段
////////////////////////////////////////////////////////////////////////////////
// 函数名称:CString GetCapitalMoney( double dMoney )
// 实现功能:获得大写金额程序
// 对全局变量的影响:无
// 参数说明:
// dMoney 金额
// 返回结果说明:大写金额字符串
////////////////////////////////////////////////////////////////////////////////
CString GetCapitalMoney(double dMoney)
{
CString strMoney;
strMoney.Format ("%.2f" , dMoney);
CString strUnit = "元拾佰仟万拾佰仟亿拾佰仟";
CString strNumber = "零壹贰叁肆伍陆柒捌玖";
CString strOtherUnit = "整角分";
int nPos = strMoney.Find (".");
int nLength = strMoney.GetLength ();
if(nPos < 0)
nPos = nLength;
CString strReturnValue;
int nCount = 0;
bool bZero = false;
bool bNeedLevel = false;
for(int i = nPos - 1;i >= 0;i --)
{
TCHAR ch = strMoney.GetAt (i);
if(nCount % 4 == 0 && nCount > 0)
{
bNeedLevel = true;
}
if(ch == '0')
{
if(nCount % 4 != 0)
bZero = true;
}
else
{
CString strTemp(strReturnValue);
strReturnValue = strNumber.Mid ((ch - 0x30) * 2 , 2);
if(nCount > 0)
{
strReturnValue += strUnit.Mid (nCount * 2 , 2);
if(nCount % 4 != 0 && bNeedLevel)
{
strReturnValue += strUnit.Mid (int(nCount / 4) * 8 , 2);
}
bNeedLevel = false;
}
if(bZero)
{
//只有比当前处理的位要低中有数字才补零
if(!strTemp.IsEmpty ())
strReturnValue += strNumber.Left (2);
bZero = false;
}
strReturnValue += strTemp;
}
nCount ++;
}
strReturnValue += strUnit.Left (2);
bool bAllZero = true;
if(nPos < nLength)
{
if(nLength > 2)
nLength = 2;
for(int i = 0;i < nLength;i ++)
if(strMoney.GetAt (nPos + i + 1) != '0')
bAllZero = false;
}
if(bAllZero)
{
strReturnValue += strOtherUnit.Left (2);
}
else
{
for(int i = 0;i < nLength;i ++)
{
TCHAR ch = strMoney.GetAt (nPos + 1 + i);
if(ch == '0' && i > 0)
{
}
else
{
strReturnValue += strNumber.Mid ((ch - 0x30) * 2 , 2);
if(ch != '0')
strReturnValue += strOtherUnit.Mid ((i + 1) * 2 , 2);
}
}
}
return strReturnValue;
}