format a number with leading and trailing zeros

format a number with leading and trailing zeros

am 15.08.2007 14:38:32 von 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

Re: format a number with leading and trailing zeros

am 15.08.2007 14:48:52 von JT

absmienk@hotmail.com wrote:
> 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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

Re: format a number with leading and trailing zeros

am 15.08.2007 14:50:41 von Paul Lalli

On Aug 15, 8:38 am, absmi...@hotmail.com wrote:
> 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

Re: format a number with leading and trailing zeros

am 15.08.2007 15:16:01 von absmienk

On Aug 15, 2:48 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> absmi...@hotmail.com wrote:
> > 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
> --
> \ Jens Thoms Toerring ___ j...@toerring.de
> \__________________________ http://toerring.de



Thanks Jens,

this really helps me.

Cheers,
Ab

Re: format a number with leading and trailing zeros

am 15.08.2007 15:18:45 von absmienk

On Aug 15, 2:50 pm, Paul Lalli wrote:
> On Aug 15, 8:38 am, absmi...@hotmail.com wrote:
>
> > 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


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