Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Fixed number of digits printing of integers (ex: 0001 to 0100)

Reply
Thread Tools

Fixed number of digits printing of integers (ex: 0001 to 0100)

 
 
Michel Rouzic
Guest
Posts: n/a
 
      12-09-2005
I can't think of a simple way to print integers with a fix number of
digits, in order to obtain something like 0001 for 1 and 0100 for 100
automatically just by specifying in a variable the number of desired
digits.

I thought about printing to a variable using for example %4d and then
replace ' ' by '0', but I don't know if i can print to an array, and
then I don't think I can put a variable between % and d to specify the
number of digits I want.

I thought that maybe there would be some already existing and easy way
to do it that I don't know of, otherwise i'd like to know how people
usually deal with that problem, thanks

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      12-09-2005
Michel Rouzic a écrit :
> I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.
>
> I thought about printing to a variable using for example %4d and then
> replace ' ' by '0', but I don't know if i can print to an array, and
> then I don't think I can put a variable between % and d to specify the
> number of digits I want.
>
> I thought that maybe there would be some already existing and easy way
> to do it that I don't know of, otherwise i'd like to know how people
> usually deal with that problem, thanks
>


#include <stdio.h>
int main(void)
{
for (int i=4; i<10;i++)
printf("%05d\n",i*100);
int width=6;
printf("%0*d\n",width,746);
}

Output
00400
00500
00600
00700
00800
00900
000746
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      12-09-2005

"Michel Rouzic" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.
>
> I thought about printing to a variable using for example %4d and then
> replace ' ' by '0', but I don't know if i can print to an array, and
> then I don't think I can put a variable between % and d to specify the
> number of digits I want.
>
> I thought that maybe there would be some already existing and easy way
> to do it that I don't know of, otherwise i'd like to know how people
> usually deal with that problem, thanks


#include <stdio.h>

int main()
{
int i = 0;

for(i = 1; i < 101; ++i)
printf("%04d\n", i);

return 0;
}

Where's your textbook?

-Mike


 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      12-09-2005

Michel Rouzic wrote:
> I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.
>
> I thought about printing to a variable using for example %4d and then
> replace ' ' by '0', but I don't know if i can print to an array, and
> then I don't think I can put a variable between % and d to specify the
> number of digits I want.
>
> I thought that maybe there would be some already existing and easy way
> to do it that I don't know of, otherwise i'd like to know how people
> usually deal with that problem, thanks


Try "%04d"

-David

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-09-2005
"Michel Rouzic" <(E-Mail Removed)> writes:

> I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.
>
> I thought about printing to a variable using for example %4d and then
> replace ' ' by '0', but I don't know if i can print to an array, and
> then I don't think I can put a variable between % and d to specify the
> number of digits I want.
>
> I thought that maybe there would be some already existing and easy way
> to do it that I don't know of, otherwise i'd like to know how people
> usually deal with that problem, thanks


int num_digits = 4;
int value = ...whatever...;
printf("%0*d\n", num_digits, value);

You should really get a C reference manual.
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
 
Reply With Quote
 
Kleuskes & Moos
Guest
Posts: n/a
 
      12-09-2005
On Fri, 09 Dec 2005 08:20:54 -0800, Michel Rouzic wrote:

> I can't think of a simple way to print integers with a fix number of
> digits, in order to obtain something like 0001 for 1 and 0100 for 100
> automatically just by specifying in a variable the number of desired
> digits.


sprintf("%04d", some_int);

 
Reply With Quote
 
Michel Rouzic
Guest
Posts: n/a
 
      12-09-2005

Mike Wahler wrote:
> "Michel Rouzic" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> >I can't think of a simple way to print integers with a fix number of
> > digits, in order to obtain something like 0001 for 1 and 0100 for 100
> > automatically just by specifying in a variable the number of desired
> > digits.
> >
> > I thought about printing to a variable using for example %4d and then
> > replace ' ' by '0', but I don't know if i can print to an array, and
> > then I don't think I can put a variable between % and d to specify the
> > number of digits I want.
> >
> > I thought that maybe there would be some already existing and easy way
> > to do it that I don't know of, otherwise i'd like to know how people
> > usually deal with that problem, thanks

>
> #include <stdio.h>
>
> int main()
> {
> int i = 0;
>
> for(i = 1; i < 101; ++i)
> printf("%04d\n", i);
>
> return 0;
> }
>
> Where's your textbook?


Thanks. Got no textbook. n869.pdf is all I got. Plus i'm learning C on
my own

 
Reply With Quote
 
Michel Rouzic
Guest
Posts: n/a
 
      12-09-2005

Ben Pfaff wrote:
> "Michel Rouzic" <(E-Mail Removed)> writes:
>
> > I can't think of a simple way to print integers with a fix number of
> > digits, in order to obtain something like 0001 for 1 and 0100 for 100
> > automatically just by specifying in a variable the number of desired
> > digits.
> >
> > I thought about printing to a variable using for example %4d and then
> > replace ' ' by '0', but I don't know if i can print to an array, and
> > then I don't think I can put a variable between % and d to specify the
> > number of digits I want.
> >
> > I thought that maybe there would be some already existing and easy way
> > to do it that I don't know of, otherwise i'd like to know how people
> > usually deal with that problem, thanks

>
> int num_digits = 4;
> int value = ...whatever...;
> printf("%0*d\n", num_digits, value);
>
> You should really get a C reference manual.


OK thanks. All i got is n869.pdf, otherwise I have nothing else. What
else should I get?

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-09-2005
"Michel Rouzic" <(E-Mail Removed)> writes:

> Ben Pfaff wrote:
>> int num_digits = 4;
>> int value = ...whatever...;
>> printf("%0*d\n", num_digits, value);
>>
>> You should really get a C reference manual.

>
> OK thanks. All i got is n869.pdf, otherwise I have nothing else. What
> else should I get?


n869.pdf should work fine as a C reference manual. You just need
to read it carefully.
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-09-2005
Michel Rouzic wrote:
> Ben Pfaff wrote:
>> "Michel Rouzic" <(E-Mail Removed)> writes:


<snip>

>> You should really get a C reference manual.

>
> OK thanks. All i got is n869.pdf, otherwise I have nothing else. What
> else should I get?


I would recommend reading the FAQ and getting a copy of K&R2.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Using %x to format number to hex and number of digits Tim Chase Python 2 11-06-2010 01:22 AM
Re: Using %x to format number to hex and number of digits Chris Rebert Python 1 11-05-2010 07:05 PM
Printing significant digits of a floating point number Christoph Bartoschek C++ 6 04-21-2009 07:53 AM
number of digits in a number bejiz C++ 12 09-27-2007 09:21 PM
Datatype's to store integers as large as 100 digits psanand@gmail.com Java 2 01-20-2005 12:31 PM



Advertisments