Pading 0s in front fixed length-fields 
Author Message
 Pading 0s in front fixed length-fields



Quote:
>How do I pad 0s (or any other char for that matter) in front
>of fields when using something like printf("%4d", foo).

Your book or manual should document this. Both "%04d" and "%.4d" have the
desired effect but for different reasons. In "%04d" the 4 specifies a
mimimum field width of 4 and the 0 specifies that numeric conversions are
padded out to the field width with zeros. In "%.4d" the 4 specifies a
"precision" i.e. the minimum number of digits that must appear in the
output. The two forms produce different output in some cases, for instance
when converting the value -1 "%04d" will produce "-001" and "%.4d" will
produce "-0001".

The *printf functions are quite powerful, it is well worth getting to know
all the facilities they offer.

--
-----------------------------------------


-----------------------------------------



Wed, 21 Jul 1999 03:00:00 GMT  
 Pading 0s in front fixed length-fields



Quote:
> How do I pad 0s (or any other char for that matter) in front
> of fields when using something like printf("%4d", foo).

"%04d"

--
=========================================================================|

| "Judge all, and be prepared to be judged by all."                      |
=========================================================================|



Wed, 21 Jul 1999 03:00:00 GMT  
 Pading 0s in front fixed length-fields


Quote:



>>How do I pad 0s (or any other char for that matter) in front
>>of fields when using something like printf("%4d", foo).

>Your book or manual should document this. Both "%04d" and "%.4d" have the
>desired effect but for different reasons. In "%04d" the 4 specifies a
>mimimum field width of 4 and the 0 specifies that numeric conversions are
>padded out to the field width with zeros. In "%.4d" the 4 specifies a
>"precision" i.e. the minimum number of digits that must appear in the
>output. The two forms produce different output in some cases, for instance
>when converting the value -1 "%04d" will produce "-001" and "%.4d" will
>produce "-0001".

You might also have mentioned what to do if you want the zero padding, and
the negative sign, but you want the output to line up properly in columns, like

         0003
        -0001
         0002

This calls for both field width and precision to be specified: a width of
five, and a precision of four:

        %5.4d

It's also worth mentioning that if a number overflows its field width, rather
than do something idiotic like fill the field with *'s, printf() will sacrifice
the proper formatting instead, and produce the entire number.

I know of a particular telephone trunk switch (not made by Nortel) whose
traffic reports have the property that numbers that are too large get converted
into a field full of *'s.

The software was probably written by former IBM employees.

The tie must be straight, the shirt must be starched, the pencils must be
sharp, and the columns *have* to line up! :)



Fri, 23 Jul 1999 03:00:00 GMT  
 Pading 0s in front fixed length-fields



...

Quote:
>You might also have mentioned what to do if you want the zero padding, and
>the negative sign, but you want the output to line up properly in columns, like

>         0003
>        -0001
>         0002

>This calls for both field width and precision to be specified: a width of
>five, and a precision of four:

>        %5.4d

You might also have mentioned that you can get the same effect with any one
of:

         % .4d
         % 05d
         %0 5d

:-)

The last case is a good test for conformance since the 0 here (in both cases)
is a flag, not part of the minimum field width number. If I remember correctly
Alicia posted some code for printf and I think it would have failed on this
case. And if you want something like:

         +0003
         -0001
         +0002

change ' ' to '+' in the conversion specifiers. (speakin to the world in
general) there's a lot of good stuff in the *printf functions, it is usually
worth giving that section of your book another read. I know I've read it
many, many times. Unfortunately in "The Annotated ANSI C Standard" the
infamous missing page is the one that describes the parts of a *printf
conversion specifier and the meanings of the - + SPACE and # flags. Luckily
with my copy a (rather grotty) photocopy of the page was pasted in.

Quote:
>It's also worth mentioning that if a number overflows its field width, rather
>than do something idiotic like fill the field with *'s, printf() will sacrifice
>the proper formatting instead, and produce the entire number.

>I know of a particular telephone trunk switch (not made by Nortel) whose
>traffic reports have the property that numbers that are too large get converted
>into a field full of *'s.

I suspect that if the number is too large you rwally don't want to know what
the true value is. The *'s are easy to spot and should instil the appropriate
level of panic.

Quote:
>The software was probably written by former IBM employees.

>The tie must be straight, the shirt must be starched, the pencils must be
>sharp, and the columns *have* to line up! :)

Obvious once you realise that columns are there to impress rather than
inform. :-)

--
-----------------------------------------


-----------------------------------------



Sun, 25 Jul 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. More Fixed Length Limitations

2. Fixed Length Limitations

3. Creating a fixed-length structure

4. please help use qsort with fixed length strings

5. Best handling of non-fixed-length data??

6. Spanned Fixed-Length Records

7. Fixed-Length File I/O

8. non-fixed length array

9. strncat and fixed length strings

10. C++ problem with Fixed length data file

11. Fixed Length Strings

12. Fixed length string???

 

 
Powered by phpBB® Forum Software