/************************************/ /* 金额转换函数(pujezha.c) */ /* */ /* 功能: 将金额转换成大写. */ /************************************/
#include /*金额变换成大写*/ #include
#include #include #include
static char *lp( int j1, int count1 ) { switch(count1-j1) { case 1: return("元"); case 2: return("拾"); case 3: return("佰"); case 4: return("仟"); case 5: return("万"); case 6: return("拾"); case 7: return("佰"); case 8: return("仟"); case 9: return("亿"); } }
char *strjea(char *d1,char *str) { int count,i,j,k; char d[20]; str[0]=0; i=0; d[0]='\0'; while (d1!='.'&&d1!='\0') { d=d1; i++; } d='\0'; if ( d[0] == '-') strcat(str,"负"); if ( d[0] == '0'||(d[0] == '-' && d[1] == '0')) { strcat(str,"零元"); return(str); } count=i; for (j=0;j{ if((j==0)&&(d[j]=='0')) break; if (d[j]=='.') break; switch(d[j]) { case '0': if (j==count-1) { strcat(str,lp(j,count)); continue; } else { k=j; while(k!=count-1) { if (d[k+1]!='0') { if((count-k)==5) strcat(str,"万"); else strcat(str,"零"); break; } else { if((count-k)==5 && (count<9 || (count==9&&(d[k-1]!='0'||d[k-2]!='0'||d[k-3]!='0')))) strcat(str,"万"); k++; if (k==count-1) goto lp1; } } j=k; continue; lp1: strcat(str,lp(k,count)); j=count; break; } case '1': strcat(str,"壹"); strcat(str,lp(j,count)); break; case '2': strcat(str,"贰"); strcat(str,lp(j,count)); break; case '3': strcat(str,"叁"); strcat(str,lp(j,count)); break; case '4': strcat(str,"肆"); strcat(str,lp(j,count)); break; case '5': strcat(str,"伍"); strcat(str,lp(j,count)); break; case '6': strcat(str,"陆"); strcat(str,lp(j,count)); break; case '7': strcat(str,"柒"); strcat(str,lp(j,count)); break; case '8': strcat(str,"捌"); strcat(str,lp(j,count)); break; case '9': strcat(str,"玖"); strcat(str,lp(j,count)); break; default: break; } } return(str); }
static char *lpp(int j1,int count1) { switch(count1-j1) { case 1: return("分"); case 2: return("角"); default: return(""); } }
char *strjeb(char d1[], char str[]) { char a[3]; int j; int count;
str[0]=0; strcpy(a,&d1[strlen(d1)-2]); if ( !strcmp(a,"00")) { strcat(str,"整"); return(str); } count=2; for(j=0;jswitch(a[j]){ case '0': if(j == 0 ) strcat(str,"零"); break; case '1': strcat(str,"壹"); strcat(str,lpp(j,count)); break; case '2': strcat(str,"贰"); strcat(str,lpp(j,count)); break; case '3': strcat(str,"叁"); strcat(str,lpp(j,count)); break; case '4': strcat(str,"肆"); strcat(str,lpp(j,count)); break; case '5': strcat(str,"伍"); strcat(str,lpp(j,count)); break; case '6': strcat(str,"陆"); strcat(str,lpp(j,count)); break; case '7': strcat(str,"柒"); strcat(str,lpp(j,count)); break; case '8': strcat(str,"捌"); strcat(str,lpp(j,count)); break; case '9': strcat(str,"玖"); strcat(str,lpp(j,count)); break; default: break; } if ( a[1]=='0' && a[0] != '0' ) strcat(str,"整"); return(str); }
char *pujezh(char *je) { static char str[80]; char tmp[80];
memset(str,0,80); sprintf(je,"%.2lf",atof(je)); memset(tmp,0,80); sprintf(str,"%s",strjea(je,tmp)); memset(tmp,0,80); sprintf(str,"%s%s",str,strjeb(je,tmp)); return(str); }
/* 金额加逗点 */ char *changje(char *je) { int len,ds,ws,i,k,zfbz=0; double jinje; char tmp[5]; static char str[32];
jinje=atof(je); if(jinje<0.00) zfbz=1; sprintf(je,"%.2lf",jinje); memset(str,0,32); len=strlen( je ); ds=(len-4-zfbz)/3; // , 个数 if( ds <= 0 ) return(je); ws=len-ds*3-3; //首个 , 前数字个数 for(k=0;kstr[k]=*je++; str[k]=','; for(i=0;i<=ds;i++) { for(k=0;k<3;k++) str[ws+k+i*3+i+1]=*je++; if( ds-i>1 ) str[ws+k+i*3+i+1]=','; else ws--; } return(str); }
/* 汉字显示金额 */ char *chsje(double je, char *str) { char buf[32]; char tmp[80]; sprintf(buf,"%.2lf",je); tmp[0]=0; strcpy(str,strjea(buf,tmp)); tmp[0]=0; sprintf(str,"%s%s",str,strjeb(buf,tmp)); return(str); }
char *fmtje(double je, char *str) { char buf[32]; sprintf(buf,"%.2lf",je); strcpy(str,changje(buf)); return(str); }
| | |