format a number with leading and trailing zeros

A

absmienk

Very simpel question. I want to format the number 2.3 to be printed as
00002.30000.

I tried this:
printf("%05d",2.3);
which produces 00002

and this:
printf("%.5f",2.3);
which produces 2.30000

How can I combine these formats to print: 00002.30000? I already
tgried abount a zillion combinations, but no luck so far. Any help is
appreciated. Should be simple, right?

Cheers,
Ab
 
J

Jens Thoms Toerring

Very simpel question. I want to format the number 2.3 to be printed as
00002.30000.
I tried this:
printf("%05d",2.3);
which produces 00002
and this:
printf("%.5f",2.3);
which produces 2.30000

Combine both your attempts:

printf "%011.5\n, 2.3;

11 is the total number of chars to output (including the dot!) and
the 0 in front of it tells printf to left-fill with '0's instead of
spaces.
Regards, Jens
 
P

Paul Lalli

Very simpel question. I want to format the number 2.3 to be printed as
00002.30000.

I tried this:
printf("%05d",2.3);
which produces 00002

and this:
printf("%.5f",2.3);
which produces 2.30000

How can I combine these formats to print: 00002.30000? I already
tgried abount a zillion combinations, but no luck so far. Any
help is appreciated. Should be simple, right?

The number that comes before the decimal in a format specifier does
not mean the same thing as the number that comes after the decimal.
The number after the decimal is how many digits past the decimal you
want to print. The number before the decimal is the TOTAL WIDTH of
the string you want to print out. Therefore, if you say %05.5f,
you're saying you want five digits past the decimal, and that you want
the entire number to take up five places. Obviously, "2.30000" does
take up at least 5 places, so that requirement is fulfilled. What you
need to do is require enough spaces so that the ones on the left total
5. "2.30000" is seven characters long, and you want four zeroes on
the left, so you need a total of 11 spaces to fill:

$ perl -e'printf("%011.5f\n", 2.3)'
00002.30000

perldoc -f sprintf
contains the full description of what the format specifier components
do.

Paul Lalli
 
A

absmienk

Combine both your attempts:

printf "%011.5\n, 2.3;

11 is the total number of chars to output (including the dot!) and
the 0 in front of it tells printf to left-fill with '0's instead of
spaces.
Regards, Jens



Thanks Jens,

this really helps me.

Cheers,
Ab
 
A

absmienk

The number that comes before the decimal in a format specifier does
not mean the same thing as the number that comes after the decimal.
The number after the decimal is how many digits past the decimal you
want to print. The number before the decimal is the TOTAL WIDTH of
the string you want to print out. Therefore, if you say %05.5f,
you're saying you want five digits past the decimal, and that you want
the entire number to take up five places. Obviously, "2.30000" does
take up at least 5 places, so that requirement is fulfilled. What you
need to do is require enough spaces so that the ones on the left total
5. "2.30000" is seven characters long, and you want four zeroes on
the left, so you need a total of 11 spaces to fill:

$ perl -e'printf("%011.5f\n", 2.3)'
00002.30000

perldoc -f sprintf
contains the full description of what the format specifier components
do.

Paul Lalli


Thanks a bunch Paul. You not only gave me the answer but a good
explanation and a lead to get the answer right away.

Ab
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top