CSDN博客

img bohut

价格数字转换成大写汉字的一个类

发表于2004/10/29 10:58:00  1380人阅读

分类: VC/MFC

/****************************以下是头文件******************************/
class CNumToHan 
{
public:
 CString Chang(CString num);
 CString ChangNum(int num);
 CNumToHan();
 virtual ~CNumToHan();

};

/****************************以下是实现文件******************************/
#include "stdafx.h"
#include "NumToHan.h"

CNumToHan::CNumToHan()
{

}

CNumToHan::~CNumToHan()
{

}
CString CNumToHan::ChangNum(int num)
{
 if(num<0||num>9)return "错";
 CString str;
 str.Format("%d",num);
 CString s="0123456789";
 CString h[10]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
 return h[s.Find(str,0)];
}

CString CNumToHan::Chang(CString num)
{
 CString str,str1;
 CString m_cUint[16]={"毫","厘","分","角","元","拾","佰","仟","万",
  "拾","佰","仟","亿","拾","佰","仟"};
 int m_iIn;
 int m_iLoc;//小数点位置
 int m_iZero=0;
 bool m_bZeroEnd=false;
 if(num.Find(".",0)==-1)
  m_iLoc=num.GetLength();
 else
  m_iLoc=num.Find(".",0);
 
 for(int i=0;i<num.GetLength();i++)
 {
  if(num.GetAt(i)=='.')continue;
  if(num.GetAt(i)=='0')
  {
   if(i==0)continue;
   m_iZero++;
   if(num.GetLength()==(i+1))
    m_bZeroEnd=true;
  }
  else
   if(m_iZero>0)m_bZeroEnd=true;
  str=num.GetAt(i);
  m_iIn=atoi(str);

  if(m_iZero<=0||m_bZeroEnd)
  {
   if(m_iZero>0&&m_iIn>0){str1+="零";m_iZero=0;m_bZeroEnd=false;}
  str1+=ChangNum(m_iIn);
  if(m_iZero>0&&m_iIn==0){str1=str1.Left(str1.GetLength()-2);m_iZero=0;}
  str1+=m_cUint[m_iLoc+3];
  }
  if((m_iLoc==9||m_iLoc==5)&&m_iZero>0)
  {
   bool m_b=true;
   if((str1.Right(2)=="亿"&&m_cUint[m_iLoc+3]=="万"))
   m_b=false;
   
   if(m_b)str1+=m_cUint[m_iLoc+3];
   m_iZero=0;
  }

  m_iLoc--;
 }
 return str1;
}

阅读全文
0 0

相关文章推荐

img
取 消
img