View previous topic :: View next topic |
Author |
Message |
iw2nzm
Joined: 23 Feb 2007 Posts: 55
|
printf %3.1w leading zeros |
Posted: Fri Apr 06, 2007 5:50 am |
|
|
Hi!
I need to print an integer in this format: 00.0
I use printf %3.1w and it works but it doesn't print the leading zero. For example:
value = 4.2 -> output = 4.2
I'd like output = 04.2
How should I fix it?
Thanks
Marco / iw2nzm |
|
|
Ttelmah Guest
|
|
Posted: Fri Apr 06, 2007 7:05 am |
|
|
Two things. On all the numeric formats, you add a leading 'zero' to the format, to make the extra leading spaces become filled with zeros. However as specified, you have no leading spaces, since your field 'width', matches the output (4.2, is three characters wide).
So you need to use:
printf("%04.1w",val);
To output a four character wide field, with a leading zero.
Best Wishes |
|
|
iw2nzm
Joined: 23 Feb 2007 Posts: 55
|
|
Posted: Fri Apr 06, 2007 8:09 am |
|
|
Ttelmah wrote: | Two things. On all the numeric formats, you add a leading 'zero' to the format, to make the extra leading spaces become filled with zeros. However as specified, you have no leading spaces, since your field 'width', matches the output (4.2, is three characters wide).
So you need to use:
printf("%04.1w",val);
Best Wishes |
Thank you very much, now it works fine!
Marco / iw2nzm |
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Fri Apr 06, 2007 1:10 pm |
|
|
I noticed a big speed impact using %4.1w over %u02.%u1 showing the quotient and rest of a division.
Maybe the compiler uses 16/16 in its internal routines. |
|
|
Ttelmah Guest
|
|
Posted: Fri Apr 06, 2007 2:30 pm |
|
|
You will notice, that %w, does not need a 'L' specifier. it assumes that you will be using a 'long', since otherwise the range is so limited.
Best Wishes |
|
|
|