View Single Post
  #2 (permalink)  
Old February 21st, 2004, 05:15 AM
Gert Gert is offline
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi. I use the following code to do this in Visual C++. Maybe you can use it. I am sure there are better ways to do this, but it works for my purposes. (It is not my code, but you may use it...)

Code:
CString CYourClass::IntToString(int number)
{
    int i=0;
    int antdes=0;    // = number of digits
    CString temp;
    BOOL negsign=FALSE;

    if(number<0)
    {
        negsign=TRUE;
        number=abs(number);
    }

    temp.Empty();
    if(number>9999999999999999999)
        antdes=20;
    else if(number>999999999999999999)
        antdes=19;
    else if(number>99999999999999999)
        antdes=18;
    else if(number>9999999999999999)
        antdes=17;
    else if(number>999999999999999)
        antdes=16;
    else if(number>99999999999999)
        antdes=15;
    else if(number>9999999999999)
        antdes=14;
    else if(number>999999999999)
        antdes=13;
    else if(number>99999999999)
        antdes=12;
    else if(number>9999999999)
        antdes=11;
    else if(number>999999999)
        antdes=10;
    else if(number>99999999)
        antdes=9;
    else if(number>9999999)
        antdes=8;
    else if(number>999999)
        antdes=7;
    else if(number>99999)
        antdes=6;
    else if(number>9999)
        antdes=5;
    else if(number>999)
        antdes=4;
    else if(number>99)
        antdes=3;
    else if(number>9)
        antdes=2;
    else 
        antdes=1;
    for(i=0;i<antdes;i++)
    {
        temp+=(char)((number%10)+48);
        number/=10;
    }
    if(negsign)
        temp+="-";
    temp.MakeReverse();
    return temp;
}
Gert
Reply With Quote