本文所讲要实现的功能是采用C++将人民币阿拉伯数字转成中文大写,具体功能如下:
#include <stdio.h>
#include <math.h>
const char mnUnit[3][2]={"分","角","元"};
const char OtherWords[2][2]={"整","负"};
const char hzUnit[8][2]={"拾","佰","仟","万","拾","佰","仟","亿"};
const char hzNum[10][2]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
extern "C" __declspec(dllexport) unsigned char __stdcall Money2ChineseCapital2(const double Num, const unsigned char BufferLength, char *Buffer ) { char szChMoney[33*4+1],*szNum; int iLen,iLen2, iNum, iAddZero=0; char buff[33]; char *buf=buff; sprintf( buf,"%0.0f",(double)(__int64)(Num*100.0+0.5*(Num<0.0?(-1):1))); iLen=strlen(buf); szNum=szChMoney; if(Num<0.0) { *(szNum++)=OtherWords[1][0]; *(szNum++)=OtherWords[1][1]; buf++; iLen--; }
for(int i=1;i<=iLen;i++) { iNum=buf[i-1]-48; iLen2=iLen-i; if(iNum==0) { if((iLen2-2)%4==0&&(iLen2-3)>0&&((iLen2>=8)||iAddZero<3)) { *(szNum++)=hzUnit[(iLen2-3)%8][0]; *(szNum++)=hzUnit[(iLen2-3)%8][1]; } iAddZero++; if(iLen>1&&iLen2==1) { *(szNum++)=hzNum[0][0]; *(szNum++)=hzNum[0][1]; } } else { if((iAddZero&&iLen2>=2)&&((iLen2-1)%4!=0)||(iAddZero>=4&&(iLen2-1)>0)) { *(szNum++)=hzNum[0][0]; *(szNum++)=hzNum[0][1]; } *(szNum++)=hzNum[iNum][0]; *(szNum++)=hzNum[iNum][1]; iAddZero=0; } if((iAddZero<1)||(iLen2==2)) { if(iLen-i>=3) { *(szNum++)=hzUnit[(iLen2-3)%8][0]; *(szNum++)=hzUnit[(iLen2-3)%8][1]; } else { *(szNum++)=mnUnit[(iLen2)%3][0]; *(szNum++)=mnUnit[(iLen2)%3][1]; } } } int Result=szNum-szChMoney; if(Result+(Num<0.0?(-2):0)==0) { *(szNum++)=hzNum[0][0]; *(szNum++)=hzNum[0][1]; *(szNum++)=mnUnit[2][0]; *(szNum++)=mnUnit[2][1]; *(szNum++)=OtherWords[0][0]; *szNum=OtherWords[0][1]; Result+=6; } else if(buf[iLen+(Num<0.0?1:0)-1]=='0') { *(szNum++)=OtherWords[0][0]; *szNum=OtherWords[0][1]; Result+=2; } if(BufferLength) { if(BufferLength>0) Result=(Result>BufferLength)?BufferLength:Result; memcpy(Buffer,szChMoney,Result); } return Result; } |