user-0 提问: 一个蛋疼的数字转字符串的问题

回复:
user: user-1
content: 既然说到秀：#include <afx.h> #include <stdio.h> CString ChineseCapitalMoney(double Num) {     CString szChMoney,szNum;     int iLen, iNum, iAddZero=0;     TCHAR* hzUnit[18]={_T("分"),_T("角"),_T("元"),_T("拾"),_T("佰"),_T("仟"),_T("万"),_T("拾"),_T("佰"),_T("仟"),_T("亿"),_T("拾"),_T("佰"),_T("仟"),_T("万"),_T("拾"),_T("佰"),_T("仟")};     TCHAR* hzNum[10]={_T("零"),_T("壹"),_T("贰"),_T("叁"),_T("肆"),_T("伍"),_T("陆"),_T("柒"),_T("捌"),_T("玖")};     if (-0.01<Num && Num<0.01) return _T("零元整");     szNum.Format(_T("%18.0f"), Num*100); // 最小到分     szNum.TrimLeft();     iLen=szNum.GetLength();     if(iLen>15 || iLen==0 || Num<0) return _T(""); // 数据错误返回      for(int i=0;i<iLen;i++) {         iNum=_ttoi((LPCTSTR)szNum.Mid(i,1));         if (iNum==0) {             iAddZero++;         } else {             if (iAddZero>0) szChMoney+=_T("零");             szChMoney+=hzNum[iNum];             iAddZero=0;         }         if (iNum!=0 || iLen-i==3 || iLen-i==11 || ((iLen-i+1)%8==0 && iAddZero<4)) szChMoney+=hzUnit[iLen-i-1];     }     if (szNum.Right(2)==_T("00")) szChMoney+=_T("整"); // 没有角和分     return szChMoney; } void main() {     double d;     CString s;      d=-1.0            ;printf("%16.2f %s\n",d,ChineseCapitalMoney(d));     d=0.0             ;printf("%16.2f %s\n",d,ChineseCapitalMoney(d));     d=1234501234567.89;printf("%16.2f %s\n",d,ChineseCapitalMoney(d));     d=1000000000000.01;printf("%16.2f %s\n",d,ChineseCapitalMoney(d));     d=1000000000001.00;printf("%16.2f %s\n",d,ChineseCapitalMoney(d));     d= 130040600090.01;printf("%16.2f %s\n",d,ChineseCapitalMoney(d)); } //           -1.00 //            0.00 零元整 //1234501234567.89 壹万贰仟叁佰肆拾伍亿零壹佰贰拾叁万肆仟伍佰陆拾柒元捌角玖分 //1000000000000.01 壹万亿元零壹分 //1000000000001.00 壹万亿零壹元整 // 130040600090.01 壹仟叁佰亿零肆仟零陆拾万零玖拾元零壹分  #include <stdio.h> #include <string.h> char hznum[100]; char hzunit[15][3]={"","十","百","千","万","十","百","千","亿","十","百","千","万","十","百"}; char hzdigit[10][3]={"零","一","二","三","四","五","六","七","八","九"}; char numstr[20]; char *ReadNumByHZ(double v) {     char *p,*d;     int u,i;      sprintf(numstr,"%.15lg",v);     printf("%s\n",numstr);     if ('-'==numstr[0]) {         sprintf(hznum,"不支持读负数");         return hznum;     }     p=strchr(numstr,'e');     if (NULL!=p) {         sprintf(hznum,"不支持读太大或太小的数");         return hznum;     }     d=NULL;     p=strchr(numstr,'.');     if (NULL!=p) {         p[0]=0;         d=p+1;     }     u=strlen(numstr);     hznum[0]=0;     for (i=0;i<u;i++) {         if ('0'==numstr[i] && 0!=((u-1-i)%4)) {             sprintf(hznum,"%s%s",hznum,hzdigit[numstr[i]-'0']);         } else {             sprintf(hznum,"%s%s%s",hznum,hzdigit[numstr[i]-'0'],hzunit[u-1-i]);         }     }     printf("%s\n",hznum);     u=strlen(hznum);     for (i=0;i<u;i+=2) {         if (0==strncmp(hznum+i,"零零",4)) {//多个连续的“零”改成一个“零”             memmove(hznum+i,hznum+i+2,u-i-2+1);             i-=2;             u-=2;         }     }     printf("%s\n",hznum);     if (0==strcmp(hznum+u-2,"零")) {//末尾“零”去掉         hznum[u-2]=0;         u-=2;     }     printf("%s\n",hznum);     for (i=0;i<u;i+=2) {         if (0==strncmp(hznum+i,"零万",4) || 0==strncmp(hznum+i,"零亿",4)) {//“万”“亿”前是“零”去掉             memmove(hznum+i,hznum+i+2,u-i-2+1);             u-=2;         }     }     printf("%s\n",hznum);     if (NULL!=d) {         sprintf(hznum,"%s点",hznum);         i=0;         while (1) {             if (0==d[i]) break;             sprintf(hznum,"%s%s",hznum,hzdigit[d[i]-'0']);             i++;         }     }     printf("%s\n",hznum);     return hznum; } void main() {     printf("\n%s\n",ReadNumByHZ(130040600090.012)); } //130040600090.012 //一千三百零零亿四千零六十零万零零九十零 //一千三百零亿四千零六十零万零九十零 //一千三百零亿四千零六十零万零九十 //一千三百亿四千零六十万零九十 //一千三百亿四千零六十万零九十点零一二 // //一千三百亿四千零六十万零九十点零一二  那就一秀到底裤吧

user: user-1
content: 6楼代码略优化： 删除第3行#include <string.h> 原第69行if (p[strlen(p)-1]==' ') p[strlen(p)-1]=0;改为if (p[-1]==' ') p[-1]=0;

user: user-1
content: Ready——Go！ DUANG！ Perfect！ #pragma warning(disable:4996) #include <stdio.h> #include <string.h> char *NUMBERS_UP_TO_20[] ={     "zero"   ,"one"    ,"two"     ,"three"   ,"four"    ,     "five"   ,"six"    ,"seven"   ,"eight"   ,"nine"    ,     "ten"    ,"eleven" ,"twelve"  ,"thirteen","fourteen",     "fifteen","sixteen","sventeen","eighteen","nineteen", }; char *NUMBERS_MULTIPLE_OF_10[] ={     "twenty", "thirty" , "fourty", "fifty" ,     "sixty" , "seventy", "eighty", "ninety", }; char *NumberToString(long long int nNum,char *p) {     char *q;     int n;     long long int nCrntNumber;      q=p;     if (nNum<0) return "";     if (nNum<20) return NUMBERS_UP_TO_20[nNum];      nCrntNumber=nNum/1000000000;     if (nCrntNumber>0) {         n=sprintf(p,"%s billion ",NumberToString(nCrntNumber,p));         p+=n;         nNum%=1000000000;     }      nCrntNumber=nNum/1000000;     if (nCrntNumber>0) {         n=sprintf(p,"%s million ",NumberToString(nCrntNumber,p));         p+=n;         nNum%=1000000;     }      nCrntNumber=nNum/1000;     if (nCrntNumber>0) {         n=sprintf(p,"%s thousand ",NumberToString(nCrntNumber,p));         p+=n;         nNum%=1000;     }      nCrntNumber=nNum/100;     if (nCrntNumber>0) {         n=sprintf(p,"%s hundred ",NumberToString(nCrntNumber,p));         p+=n;         nNum%=100;     }      if (0<nNum && nNum<20) {         n=sprintf(p,"%s ",NumberToString(nNum,p));         p+=n;     } else {         nCrntNumber=nNum/10;         if (nCrntNumber>0) {             n=sprintf(p,"%s ",NUMBERS_MULTIPLE_OF_10[nCrntNumber-2]);             p+=n;             nNum%=10;         }          nCrntNumber=nNum;         if (nCrntNumber>0) {             n=sprintf(p,"%s ",NumberToString(nCrntNumber,p));             p+=n;         }     }      if (p[strlen(p)-1]==' ') p[strlen(p)-1]=0;      return q; } int main() {     char sRes[256];     long long int d;      d=                  0  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                  1  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                  9  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                 19  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                 20  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                 21  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                 90  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                100  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=               2000  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=            3000000  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=         4000000000ll;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=1000000000000000002ll;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=1234567890123456789ll;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=9223372036854775807ll;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));//2^63-1     return 0; } //                  0 is [zero] //                  1 is [one] //                  9 is [nine] //                 19 is [nineteen] //                 20 is [twenty] //                 21 is [twenty one] //                 90 is [ninety] //                100 is [one hundred] //               2000 is [two thousand] //            3000000 is [three million] //         4000000000 is [four billion] //1000000000000000002 is [one billion billion two] //1234567890123456789 is [one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety billion one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine] //9223372036854775807 is [nine billion two hundred twenty three million three hundred seventy two thousand thirty six billion eight hundred fifty four million seven hundred seventy five thousand eight hundred seven] //

user: user-2
content: char s_n[20] = { "", "One",...,"Nineteen"}; char s_n1[10]= { "", "", "twenty",...,"ninety"};  void thousandToStr( UINT u, char *pStr ) { 	UINT u1 = u/100; 	if( u1 > 0 ) 	{ 		strcat( pStr, s_n[u1] ); 		strcat( pStr, " hundred " ); 	} 	u = u%100; 	u1= u/10; 	if( u1 > 1 ) 	{ 		strcat( pStr, s_n1 ); 		strcat( pStr, " " ); 	} 	u1 = u%20; 	strcat( pStr, s_n ); } void MillionToStr( UINT u, char *pStr ) { 	UINT u1 = u/1000000; 	if( u1 > 0 ) 	{ 		MillionToStr( u1, pStr ); 		strcat( pStr, " Million ") 	} 	u1 = u/1000; 	if( u1 > 0 ) 	{ 		thousandToStr( u1, pStr ); 		strcat( pStr, " thousand" ); 	} 	strcat( pStr, " " ); 	thousandToStr( u1, pStr ); }  void UintToStr( UINT u, char* pStr ) { 	MillionToStr( u, pStr ); }

user: user-3
content: #include <stdio.h> #include <string.h> #include <memory.h>  #pragma warning (disable : 4996)  void written_amount(unsigned long long amount, char * buffer); char* head(unsigned long long num, char* buf);  char* _1[]  = {"", " thousand", " million", " billion"}; char* _2[]  = {"", " ten"," twenty"," thirty"," forty"," fifty"," sixty"," seventy"," eighty"," ninety"}; char* _3[]  = {"", " one"," two"," three"," four"," five"," six"," seven"," eight"," nine", " ten", " eleven"," twelve"," thirteen"," fourteen"," fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};  int main(void) {     unsigned long long n;     char str[255];     memset(str, '\0', 254);      printf("Please enter the value.\n");     scanf("%I64d", &n);      if (n > 999999999999)     {         printf("Hard to describe a number larger than 999 billion \n");         return -1;     }      written_amount(n, str);     puts(str);      return 0; }   void written_amount(unsigned long long amount, char * buffer) {     long long i = 0;     long long temp = amount;     long long bu = amount;      for (i = 3; i >= 0; i--)     {         unsigned long long temp = amount;         char stemp[128] = {0};          for (long long j = 0; j < i; j++)         {             temp /= 1000;         }         temp %= 1000;         if (temp > 0)         {             strcat(buffer, head(temp, stemp));             strcat(buffer, _1[i]);         }     } }  char* head(unsigned long long num, char* buf) {     char stemp[128] = {0};     unsigned long long temp;          //hundred     temp = num / 100;     if (temp > 0)     {         strcat(buf, _3[temp]);         strcat(buf, " hundred ");     }      //less than hundred     temp = num % 100;     if (temp > 0)     {         strcat(buf, num / 100 > 0 ? " and " : "");         if (temp > 0 && temp < 20 )     // 0-19         {             strcat(buf, _3[temp]);         }         else                            // >19         {             strcat(buf, _2[temp / 10]);             //less than 10             temp = temp % 10;             if (temp > 0)             {                 strcat(buf, _3[temp]);             }         }     }          return buf; }

user: user-1
content: 仅供参考：///////////////////////////////////////////////////////////// // FREE code from CODECENTRIX // http://www.codecentrix.com/ // http://codecentrix.blogspot.com/ ///////////////////////////////////////////////////////////// #include "stdafx.h"  const TCHAR* const NUMBERS_UP_TO_20[] = {     _T("zero"),     _T("one"),     _T("two"),      _T("three"),    _T("four"),     _T("five"),     _T("six"),     _T("seven"),    _T("eight"),    _T("nine"),     _T("ten"),      _T("eleven"),  _T("twelve"),   _T("thirteen"), _T("fourteen"),     _T("fifteen"),  _T("sixteen"), _T("sventeen"), _T("eighteen"), _T("nineteen") };  const TCHAR* const NUMBERS_MULTIPLE_OF_10[] = {     _T("twenty"), _T("thirty"),  _T("fourty"), _T("fifty"),     _T("sixty"),  _T("seventy"), _T("eighty"), _T("ninety") };   tstring NumberToStringEN(INT64 nNumber) {     if (nNumber < 0)     {         return _T("");     }      if (nNumber < 20)     {         return NUMBERS_UP_TO_20[nNumber];     }      tstring sResult;     INT64   nCrntNumber = nNumber / 1000000000;      if (nCrntNumber > 0)     {         sResult = NumberToStringEN(nCrntNumber);         sResult += _T(" billion");         nNumber %= 1000000000;     }      nCrntNumber = nNumber / 1000000;     if (nCrntNumber > 0)     {         sResult = NumberToStringEN(nCrntNumber);         sResult += _T(" million");         nNumber %= 1000000;     }      nCrntNumber = nNumber / 1000;     if (nCrntNumber > 0)     {         sResult = NumberToStringEN(nCrntNumber);         sResult += _T(" thousand");         nNumber %= 1000;     }  	nCrntNumber = nNumber / 100; 	if (nCrntNumber > 0) 	{ 		sResult = NumberToStringEN(nCrntNumber); 		sResult += _T(" hundred"); 		nNumber %= 100; 	}  	if ((0 < nNumber) && (nNumber < 20)) 	{ 		sResult += _T(" "); 		sResult += NumberToStringEN(nNumber); 	} 	else 	{ 		nCrntNumber = nNumber / 10; 		if (nCrntNumber > 0) 		{ 			sResult += _T(" "); 			sResult += NUMBERS_MULTIPLE_OF_10[nCrntNumber - 2]; 			nNumber %= 10; 		}  		nCrntNumber = nNumber; 		if (nCrntNumber > 0) 		{ 			sResult += _T(" "); 			sResult += NumberToStringEN(nCrntNumber); 		} 	}  	if (sResult[sResult.length() - 1] == _T(' ')) 	{ 		// Erase last space. 		sResult.erase(sResult.length() - 1, 1); 	}  	return sResult; }

user: user-4
content: 如果能利用库函数的话先把16312 转为16312转为字符串 “16312”(利用 sprintf)，再一个一个字符转就可以

user: user-3
content: 我觉得应该有改善的空间，因为除了 billion、million、thousand、hundred，就剩下1-9了，应该不用写那么多次分支，待我写写试试

user: user-5
content: 看各位秀代码中

user: user-6
content: 为什么不用1000作为模？

user: user-7
content: 西方计数的特点是3位一段，中国是4位一段。 所以3位一个处理就ok了。 10万用西方的话说就是100个一千。

user: user-8
content: 引用 6 楼 zhao4zhong1 的回复:Ready——Go！ DUANG！ Perfect！ #pragma warning(disable:4996) #include <stdio.h> #include <string.h> char *NUMBERS_UP_TO_20[] ={     "zero"   ,"one"    ,"two"     ,"three"   ,"four"    ,     "five"   ,"six"    ,"seven"   ,"eight"   ,"nine"    ,     "ten"    ,"eleven" ,"twelve"  ,"thirteen","fourteen",     "fifteen","sixteen","sventeen","eighteen","nineteen", }; char *NUMBERS_MULTIPLE_OF_10[] ={     "twenty", "thirty" , "fourty", "fifty" ,     "sixty" , "seventy", "eighty", "ninety", }; char *NumberToString(long long int nNum,char *p) {     char *q;     int n;     long long int nCrntNumber;      q=p;     if (nNum<0) return "";     if (nNum<20) return NUMBERS_UP_TO_20[nNum];      nCrntNumber=nNum/1000000000;     if (nCrntNumber>0) {         n=sprintf(p,"%s billion ",NumberToString(nCrntNumber,p));         p+=n;         nNum%=1000000000;     }      nCrntNumber=nNum/1000000;     if (nCrntNumber>0) {         n=sprintf(p,"%s million ",NumberToString(nCrntNumber,p));         p+=n;         nNum%=1000000;     }      nCrntNumber=nNum/1000;     if (nCrntNumber>0) {         n=sprintf(p,"%s thousand ",NumberToString(nCrntNumber,p));         p+=n;         nNum%=1000;     }      nCrntNumber=nNum/100;     if (nCrntNumber>0) {         n=sprintf(p,"%s hundred ",NumberToString(nCrntNumber,p));         p+=n;         nNum%=100;     }      if (0<nNum && nNum<20) {         n=sprintf(p,"%s ",NumberToString(nNum,p));         p+=n;     } else {         nCrntNumber=nNum/10;         if (nCrntNumber>0) {             n=sprintf(p,"%s ",NUMBERS_MULTIPLE_OF_10[nCrntNumber-2]);             p+=n;             nNum%=10;         }          nCrntNumber=nNum;         if (nCrntNumber>0) {             n=sprintf(p,"%s ",NumberToString(nCrntNumber,p));             p+=n;         }     }      if (p[strlen(p)-1]==' ') p[strlen(p)-1]=0;      return q; } int main() {     char sRes[256];     long long int d;      d=                  0  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                  1  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                  9  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                 19  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                 20  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                 21  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                 90  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=                100  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=               2000  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=            3000000  ;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=         4000000000ll;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=1000000000000000002ll;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=1234567890123456789ll;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));     d=9223372036854775807ll;printf("%19lld is [%s]\n",d,NumberToString(d,sRes));//2^63-1     return 0; } //                  0 is [zero] //                  1 is [one] //                  9 is [nine] //                 19 is [nineteen] //                 20 is [twenty] //                 21 is [twenty one] //                 90 is [ninety] //                100 is [one hundred] //               2000 is [two thousand] //            3000000 is [three million] //         4000000000 is [four billion] //1000000000000000002 is [one billion billion two] //1234567890123456789 is [one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety billion one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine] //9223372036854775807 is [nine billion two hundred twenty three million three hundred seventy two thousand thirty six billion eight hundred fifty four million seven hundred seventy five thousand eight hundred seven] //