//itow.h #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef itowh #define itowh /* Turns an integer into words. There are two versions of the class, one uses the Standard Template Library string class, the other uses the MFC CString class. The std::string version is the default. To use MFC CString Version comment out the line: #define UseSTLstring Usage: std::string Version: std::string s=Citow::itow(100100); MFC CString Version: CString S=Citow::itow(100100); */ //Comment out the next line to use MFC CString Version: #define UseSTLstring #ifdef UseSTLstring #include //This include has to happen before THIS_FILE defines in MFC class Citow { public: Citow() {} virtual ~Citow() {} // Don't allow canonical behavior (i.e. don't allow this class to be passed by value) Citow(const Citow&) {}; Citow& operator=(const Citow&) {return(*this);}; static std::string _fastcall itow(__int64 i, bool Ordinals=false) { // i =The Number, Ordinals=true="First", "Second"etc. ; False= "One", "Two" etc. static const char* Cardinal[]={"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "}; static const char* Ordinal[]={"th", "First ", "Second ", "Third ", "Fourth ", "Fifth ", "Sixth ", "Seventh ", "Eighth ", "Ninth ", "Tenth ", "Eleventh ", "Twelth ", "Thirteenth ", "Fouteenth ", "Fifteenth ", "Sixteenth ", "Seventeenth ", "Eighteenth ", "Nineteenth "}; static const char* _Tens[]={"", "Ten ", "Twenty ", "Thirty ", "Fourty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "}; static const char* HUT="HUT"; std::string op; // op=Output String. char Buf[24]; // Buf=String of the number. char* S=Buf; // S=Pointer to String of the number. char U[2]=""; // U=Units flag for two digit words(from eleven to nineteen). char F=' '; // F=Flag for addition of the words "Million", "Thousand"or "Hundred". if(i<0) { op="Minus "; _i64toa(-i, S, 10); }else _i64toa(i, S, 10); char L=strlen(S); // L=Length of the string(present digit being dealt with). while(L>0) { char T=HUT[L%3]; // T=State(doing Hundreds, Tens or Units). char c=(*S)-'0'; // c=The value of the present digit(s) being dealt with. if((c>0)&&(T=='H')) { op+=Cardinal[c]; if(c) F=c+'0'; }else if(T=='T') { if(*S<'2') {*U=*S; *(U+1)=*(S+1);} //strncpy(U,S,2); else{ if(((F!='M') || ((L-5)%6)) && !op.empty()) op+="and "; op+=_Tens[c]; F=c+'0'; } }else if(T=='U') { if(*U) { c=((*U)-'0')*10+((*(U+1))-'0'); //c=atoi(U) if(((F!='M')||(((L-4)%6)!=0))&&(!op.empty()) && c) op+="and "; *U=0; } op+=(Ordinals && (L==1) ? Ordinal[c] : Cardinal[c]); if(c) F=c+'0'; } if(L>1) { if(((L-7)%6)==0) { op+="Million "; F='M'; }else if((F!='M')&&((L-4)%6==0)) { op+="Thousand "; F='T'; }else if((F!='M')&&(F!='T')&&((L-3)%3==0)) { op+="Hundred "; F='H'; } } ++S; --L; //Next Digit } return i ? op.substr(0,op.length()-1) : (Ordinals ? "Zeroth" : "Zero"); } }; #elif //ndef UseSTLstring class Citow { public: Citow() {} virtual ~Citow() {} // Don't allow canonical behavior (i.e. don't allow this class to be passed by value) Citow(const Citow&) {}; Citow& operator=(const Citow&) {return(*this);}; static CString _fastcall itow(__int64 i, bool Ordinals=false) { // i =The Number, Ordinals=true="First", "Second"etc. ; False= "One", "Two" etc. static const char* Cardinal[]={"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "}; static const char* Ordinal[]={"th", "First ", "Second ", "Third ", "Fourth ", "Fifth ", "Sixth ", "Seventh ", "Eighth ", "Ninth ", "Tenth ", "Eleventh ", "Twelth ", "Thirteenth ", "Fouteenth ", "Fifteenth ", "Sixteenth ", "Seventeenth ", "Eighteenth ", "Nineteenth "}; static const char* _Tens[]={"", "Ten ", "Twenty ", "Thirty ", "Fourty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "}; static const char* HUT="HUT"; CString op; // op=Output String. char Buf[24]; // Buf=String of the number. char* S=Buf; // S=Pointer to String of the number. char U[2]=""; // U=Units flag for two digit words(from eleven to nineteen). char F=' '; // F=Flag for addition of the words "Million", "Thousand"or "Hundred". if(i<0) { op="Minus "; _i64toa(-i, S, 10); }else _i64toa(i, S, 10); char L=strlen(S); // L=Length of the string(present digit being dealt with). while(L>0) { char T=HUT[L%3]; // T=State(doing Hundreds, Tens or Units). char c=(*S)-'0'; // c=The value of the present digit(s) being dealt with. if((c>0)&&(T=='H')) { op+=Cardinal[c]; if(c) F=c+'0'; }else if(T=='T') { if(*S<'2') {*U=*S; *(U+1)=*(S+1);}$//strncpy(U,S,2); else{ if(((F!='M') || ((L-5)%6)) && !op.IsEmpty()) op+="and "; op+=_Tens[c]; F=c+'0'; } }else if(T=='U') { if(*U) { c=((*U)-'0')*10+((*(U+1))-'0'); //c=atoi(U) if(((F!='M')||(((L-4)%6)!=0))&&(!op.IsEmpty()) && c) op+="and "; *U=0; } op+=(Ordinals && (L==1) ? Ordinal[c] : Cardinal[c]); if(c) F=c+'0'; } if(L>1) { if(((L-7)%6)==0) { op+="Million "; F='M'; }else if((F!='M')&&((L-4)%6==0)) { op+="Thousand "; F='T'; }else if((F!='M')&&(F!='T')&&((L-3)%3==0)) { op+="Hundred "; F='H'; } } ++S; --L; //Next Digit } return i ? op.Left(op.GetLength()-1) : (Ordinals ? "Zeroth" : "Zero"); } }; #endif //ndef UseSTLstring #endif // itowh