本文共 3301 字,大约阅读时间需要 11 分钟。
最近在学习MFC,编写了个金额由小写到大写的转换,全部代码如下:/************************************************************************//* 对阿拉伯数组转换成中文数字 *//************************************************************************/CString CToChineseDlg::ToChineseLetter(CString strSourceLetter){ strSourceLetter.Replace(L"0", L"零"); strSourceLetter.Replace(L"1", L"壹"); strSourceLetter.Replace(L"2", L"贰"); strSourceLetter.Replace(L"3", L"叁"); strSourceLetter.Replace(L"4", L"肆"); strSourceLetter.Replace(L"5", L"伍"); strSourceLetter.Replace(L"6", L"陆"); strSourceLetter.Replace(L"7", L"柒"); strSourceLetter.Replace(L"8", L"捌"); strSourceLetter.Replace(L"9", L"玖"); return strSourceLetter;}void CToChineseDlg::OnBnClickedButton1(){ // 先判断是否有小数点 CString strSource, strDotAfter, strResult; GetDlgItem(IDC_EDIT_SOURCE)->GetWindowText(strSource); strDotAfter = this->GetDotAfterLength(strSource); int iAllLength = strSource.GetLength(); int iDotAfterLength = strDotAfter.GetLength(); if(iDotAfterLength == 0) { if(iAllLength > 12) { MessageBox(L"输入的数字过长,不要超过12位", L"提示"); } else { strResult = this->ChangeMoney(strSource); GetDlgItem(IDC_EDIT_RESULT)->SetWindowText(strResult); } } else { if(iAllLength - iDotAfterLength - 1 > 12) { MessageBox(L"输入的数字整数部分过长,不要超过12位", L"提示"); } else if(iDotAfterLength > 2) { MessageBox(L"输入的数字小数点后面过长,不要超过2位", L"提示"); } else { CString strDotBefore = strSource.Left(iAllLength - iDotAfterLength - 1); strResult = this->ChangeMoney(strDotBefore); // 再来计算小数点后的,这个简单不需要在封装函数了 for (int i = 0; i < iDotAfterLength; i++) { CString strUnit(""); CString strTemp = strDotAfter.Left(1); strTemp = this->ToChineseLetter(strTemp); strDotAfter = strDotAfter.Mid(1); switch (i) { case 0: strUnit = "角";break; case 1: strUnit = "分";break; } strResult += strTemp + strUnit; } GetDlgItem(IDC_EDIT_RESULT)->SetWindowText(strResult); } }}/************************************************************************//* 得到小数点后的字符串 *//************************************************************************/CString CToChineseDlg::GetDotAfterLength(CString strSource){ int iDotIndex = strSource.Find('.'); if(iDotIndex > -1) { return strSource.Mid(iDotIndex + 1); } return CString("");}/************************************************************************//* 转换 *//************************************************************************/CString CToChineseDlg::ChangeMoney(CString strSource){ CString strResult(""); int iLength = strSource.GetLength(); for (int i = 0; i < iLength; i++) { CString strUnit(""); // 获取右边第一个字符 CString strTemp = strSource.Right(1); strSource = strSource.Left(strSource.GetLength() - 1); // 得到中文 strTemp = this->ToChineseLetter(strTemp); // 得到单位 switch (i) { case 0: strUnit = "元";break; case 1: case 5: case 9: strUnit = "拾";break; case 2: case 6: case 10: strUnit = "百";break; case 3: case 7: case 11: strUnit = "千";break; case 4: strUnit = "万";break; case 8: strUnit = "亿";break; } // 拼接起来 strResult = strTemp + strUnit + strResult; } return strResult;}
效果图如下:
一下效果图是别处使用的。。
转载地址:http://woini.baihongyu.com/