468,512 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

using sprintf() with 0's

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

Aug 16 '06 #1
7 6342
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
Aug 16 '06 #2

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 '06 #3

wi******@hotmail.com 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

Aug 16 '06 #4
"markww" <ma****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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?
Aug 17 '06 #5
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
Aug 18 '06 #6
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:hI******************************@comcast.com. ..
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
Aug 18 '06 #7
markww wrote:
wi******@hotmail.com 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).

Aug 18 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by huey_jiang | last post: by
1 post views Thread by jimjim | last post: by
1 post views Thread by verge | last post: by
12 posts views Thread by Henryk | last post: by
15 posts views Thread by krister | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.