www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/28/11:44:53

From: ultrastar AT online DOT tietokone DOT fi (Niki Ruusunko)
To: djgpp AT delorie DOT com
Date: Tue, 28 Jul 1998 18:43:46 +0300
Subject: sprintf and 64 bit integers
Message-ID: <msg316796.thr-3d13781.f625b@online.tietokone.fi>
Organization: Tietokone Online
MIME-Version: 1.0

I have this problem with a function I use to do thousands separators to
integers. Here is the code:

char * LargeInt(unsigned int bignumber)
        {
        int counter = -1, i, i2 = 0, sep = 0;
        char string1[21];
        char string2[27];
        sprintf(string1, "%d", bignumber);
        i = strlen(string1);
        while(i >= 0)
                {
                if (counter < 3)
                        {
                        string2[i2] = string1[i];
                        counter++;
                        }
                else
                        {
                        string2[i2] = ',';
                        i2++;
                        string2[i2] = string1[i];
                        counter = 1;
                        sep++;
                        }
                i--;
                i2++;
                }
        i = 0;
        i2 = strlen(string1) + sep;
        while(i2 >= 0)
                {
                tmpInt[i] = string2[i2];
                i++;
                i2--;
                }
        return tmpInt;
        }

It isn't very good function but it works (on ints that is). The problem is
that I need it to support the "long long" -variables I use. Sprintf doesn't
compile with a long long as the argument (it says: "warning: int format,
different type arg (arg 3)") so I'm asking if there is an alternate
function to make a string out of an int (or, a long long in this case).

Of course, if you know how to make the separators some better way, please
tell me.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019