uCommon.cpp
上传用户:liuxiao
上传日期:2022-07-06
资源大小:1115k
文件大小:3k
- //---------------------------------------------------------------------------
- #pragma hdrstop
- #include "uCommon.h"
- //---------------------------------------------------------------------------
- #include "math.h"
- // 常量字符串定义,如修改也可变为转换普通汉字函数(如:一二三,十百千)
- const char *HanDigiStr[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
- const char *HanDiviStr[] = { "","拾","佰","仟","万","拾","佰","仟","亿",
- "拾","佰","仟","万","拾","佰","仟","亿",
- "拾","佰","仟","万","拾","佰","仟" };
- AnsiString PositiveIntegerToHanStr(String NumStr)
- {// 输入字符串必须正整数,只允许前导空格(必须右对齐),不宜有前导零
- AnsiString RMBStr;
- int len,n,lastzero=0;
- int hasvalue=0;// 亿、万进位前有数值标记
- len = NumStr.Length();
- if( len > 24 ) return "数值过大!";
- for(int i=len-1;i>=0;i--) {
- if( NumStr[len-i]==' ' ) continue; // AnsiString[] base is 1
- n = NumStr[len-i] - '0';
- if( n<0 || n>9 ) return "输入含非数字字符!";
- if( n!=0 ) {
- if( lastzero ) RMBStr += HanDigiStr[0];// 若干零后若跟非零值,只显示一个零
- // 除了亿万前的零不带到后面
- //if( !( n==1 && (i%4)==1 && (lastzero || i==len-1) ) ) // 如十进位前有零也不发壹音用此行
- //if( !( n==1 && (i%4)==1 && i==len-1 ) ) // 十进位处于第一位不发壹音
- RMBStr += HanDigiStr[n];
- RMBStr += HanDiviStr[i];// 非零值后加进位,个位为空
- hasvalue = 1; // 置万进位前有值标记
- }
- else {
- if( (i%8)==0 || ((i%8)==4 && hasvalue) ) // 亿万之间必须有非零值方显示万
- RMBStr += HanDiviStr[i];// “亿”或“万”
- }
- if( i%8==0 ) hasvalue = 0; // 万进位前有值标记逢亿复位
- lastzero = (n==0) && (i%4!=0); // 亿万前有零后不加零,如:拾万贰仟
- }
- if( RMBStr.Length()==0 ) return HanDigiStr[0]; // 输入空字符或"0",返回"零"
- return RMBStr;
- }
- AnsiString NumToRMBStr(double val)
- {
- AnsiString SignStr,TailStr;
- double fraction, integer;
- int jiao,fen;
- if( val<0 ) {
- val = -val;
- SignStr = "负";
- }
- fraction = modf(val+0.005, &integer); // 四舍五入到分
- jiao = int(fraction*10);
- fen = int(fraction*100)-jiao*10;
- if( jiao==0 && fen==0 ) {
- TailStr = "整";
- }
- else {
- TailStr = HanDigiStr[jiao];
- if( jiao!=0 )
- TailStr += "角";
- if( integer==0 && jiao==0 ) // 零元后不写零几分
- TailStr = "";
- if( fen!=0 )
- TailStr += HanDigiStr[fen] + AnsiString("分");
- }
- // 下一行可用于非正规金融场合,0.03只显示“叁分”而不是“零元叁分”
- // if( integer==0 ) return SignStr+TailStr;
- return SignStr+PositiveIntegerToHanStr(String(integer))+"元"+TailStr;
- }
- //-----------------------------------------------------------
- #pragma package(smart_init)