using sprintf() with 0's

Discussion in 'C++' started by markww, Aug 16, 2006.

  1. markww

    markww Guest

    Hi,

    Is there a way to use sprintf() to put a number of zeros before a
    number, like:

    sprintf(szBuffer, "%i", 7);

    "007";

    I have a list of numbers I want to sort that are text based, and I want
    to put zeros before some so they get sorted in order like:

    007
    008
    009
    010
    011
    012

    Thanks
     
    markww, Aug 16, 2006
    #1
    1. Advertising

  2. markww

    billy Guest

    How about sprintf (szBuffer, "%03i", 7)?

    The leading 0 of "%03i" means to print 0 in front of the number; 3
    means the field width is 3.

    markww wrote:
    > Hi,
    >
    > Is there a way to use sprintf() to put a number of zeros before a
    > number, like:
    >
    > sprintf(szBuffer, "%i", 7);
    >
    > "007";
    >
    > I have a list of numbers I want to sort that are text based, and I want
    > to put zeros before some so they get sorted in order like:
    >
    > 007
    > 008
    > 009
    > 010
    > 011
    > 012
    >
    > Thanks
     
    billy, Aug 16, 2006
    #2
    1. Advertising

  3. markww

    Guest

    markww wrote:
    > Hi,
    >
    > Is there a way to use sprintf() to put a number of zeros before a
    > number, like:
    >
    > sprintf(szBuffer, "%i", 7);
    >
    > "007";
    >
    > I have a list of numbers I want to sort that are text based, and I want
    > to put zeros before some so they get sorted in order like:
    >
    > 007
    > 008
    > 009
    > 010
    > 011
    > 012
    >
    > Thanks


    this padds it with zeros, and lenghth 3:

    sprintf(szBuffer, "%03i", 7);
     
    , Aug 16, 2006
    #3
  4. markww

    markww Guest

    wrote:
    > markww wrote:
    > > Hi,
    > >
    > > Is there a way to use sprintf() to put a number of zeros before a
    > > number, like:
    > >
    > > sprintf(szBuffer, "%i", 7);
    > >
    > > "007";
    > >
    > > I have a list of numbers I want to sort that are text based, and I want
    > > to put zeros before some so they get sorted in order like:
    > >
    > > 007
    > > 008
    > > 009
    > > 010
    > > 011
    > > 012
    > >
    > > Thanks

    >
    > this padds it with zeros, and lenghth 3:
    >
    > sprintf(szBuffer, "%03i", 7);


    That is some hot gravy, thanks guys.

    Mark
     
    markww, Aug 16, 2006
    #4
  5. markww

    Jim Langston Guest

    "markww" <> wrote in message
    news:...
    > Hi,
    >
    > Is there a way to use sprintf() to put a number of zeros before a
    > number, like:
    >
    > sprintf(szBuffer, "%i", 7);
    >
    > "007";
    >
    > I have a list of numbers I want to sort that are text based, and I want
    > to put zeros before some so they get sorted in order like:
    >
    > 007
    > 008
    > 009
    > 010
    > 011
    > 012
    >
    > Thanks


    You could do as others said, but I would probably just use stringstring to
    convert it to an int and sort that anyway. Either way you're going to need
    a temporary to sort on (in your case szBuffer, in my case an int). This
    also has the advantage of being able to sort arbitrary sized numbers. What
    happens when you get to 1000?
     
    Jim Langston, Aug 17, 2006
    #5
  6. Jim Langston wrote:
    > [..] I would probably just use
    > stringstring to [..]


    "stringstring"?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 18, 2006
    #6
  7. markww

    Jim Langston Guest

    "Victor Bazarov" <> wrote in message
    news:...
    > Jim Langston wrote:
    >> [..] I would probably just use
    >> stringstring to [..]

    >
    > "stringstring"?
    >
    > V


    You know how many times I type std::stringstring in a program and get a
    syntax error? Just about every time I use it actually.

    Of course I meant std::stringstream
     
    Jim Langston, Aug 18, 2006
    #7
  8. markww

    Old Wolf Guest

    markww wrote:
    > wrote:
    > > markww wrote:
    > > > Is there a way to use sprintf() to put a number of zeros before a
    > > > number,

    > >
    > > sprintf(szBuffer, "%03i", 7);

    >
    > That is some hot gravy, thanks guys.


    Bear in mind that the 3 doesn't limit the field width, eg:

    sprintf(buf, "%03i", 1234);

    will still write "1234". So buf needs to be big enough for the
    biggest possible integer (including 0 terminator).
     
    Old Wolf, Aug 18, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jt
    Replies:
    3
    Views:
    927
    Keith Thompson
    May 23, 2005
  2. Replies:
    7
    Views:
    959
    Maxim Yegorushkin
    Jun 15, 2006
  3. Susan Rice
    Replies:
    3
    Views:
    639
    Markus Svilans
    Jul 4, 2006
  4. Iñaki Baz Castillo

    Using sprintf() to print a Hash

    Iñaki Baz Castillo, May 24, 2011, in forum: Ruby
    Replies:
    11
    Views:
    445
    Robert Klemme
    May 24, 2011
  5. Mike Flannigan

    Matching FP Numbers and Using Sprintf

    Mike Flannigan, Aug 23, 2003, in forum: Perl Misc
    Replies:
    7
    Views:
    97
    Jürgen Exner
    Aug 25, 2003
Loading...

Share This Page