Thread: printf: zero padding float

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    201

    printf: zero padding float

    (first post here )

    With printf you can add zeroes to the output by specifying %03d like this:
    int x = 123;
    printf("%06d\n", x); => will print 000123

    How can this be achieved with floats? (if it's possible)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't, not just using printf's format specifiers anyway. At least not to the left of the decimal point. You can only specify to the right of the decimal point.
    Code:
    printf("%.3f", 1.0 );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    int main(void) 
    {
       float value = 1.0F;
       printf("value = %07.3f\n", value);
       return 0;
    }
    
    /* my output
    value = 001.000
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    But Dave's response wouldn't work with this number
    Code:
    #include <stdio.h>
    
    int main(void) 
    {
       float value = 1000000.0F;
       printf("value = %07.3f\n", value);
       return 0;
    }
    
    /* my output
    value = 000.000 
    */
    I am sure Dave knew this, I am just pointing it out.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's rather interesting. Run this:
    Code:
    #include <stdio.h>
    
    int main( void )
    {
            printf("%03.3f", 1.1 );
            return 0;
    }
    
    /*
        My output:
        1.100
    */
    Dave, change your seven to a three and watch it break. Hell, change it to a 6 and watch it break. I'm not sure what exactly you're trying to say there, because "7" does not give you seven points of precision like it would if it were an integer. The point is, it doesn't work right, and in fact, it isn't even guarinteed that it will work right. Reading the man pages, they only specify that to the right of the decimal point will work.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > printf("%03.3f", 1.1 );

    The first 3 represents the total field width.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by swoopy
    > printf("%03.3f", 1.1 );

    The first 3 represents the total field width.
    Looks like you learn something new every .... well, once in a while anyway. I was thinking it was places left and places right respectively. On an aside, the man pages do say it only works to the right.

    [edit]
    An optional precision, in the form of a period
    (`.') followed by an optional digit string. If
    the digit string is omitted, the precision is taken
    as zero. This gives the minimum number of digits
    to appear for d, i, o, u, x, and X conversions, the
    number of digits to appear after the decimal-point
    for e, E, and f conversions,
    the maximum number of
    significant digits for g and G conversions, or the
    maximum number of characters to be printed from a
    string for s conversions.
    Here was my misunderstanding.
    [/edit]

    Quzah.
    Last edited by quzah; 06-15-2004 at 05:45 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    "Undefined" is the word missing from this thread.
    0 For d, i, o, u, x, X, e, E, f, g and G conversions, leading zeros
    (following any indication of sign or base) are used to pad to the
    field width; no space padding is performed. If the 0 and - flags
    both appear, the 0 flag will be ignored. For d, i, o, u, x and X
    conversions, if a precision is specified, the 0 flag will be
    ignored. For other conversions, the behavior is undefined.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah. I thought I at least half way knew what I was talking about. I just didn't see it when looking for it a second time...

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Of course if you have something like:
    Code:
    printf("%03.3f\n",1.125);
    Even though it's five wide, at least it doesn't truncate it.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Hammer, can you explain that in English.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Width specifiers only give you the minimum width. (As with integers. It only gives you a guarinteed minimum width, but if your number is larger, it will not truncate.) So, assuming it was all defined behaviour, the value to the left would be the total guarinteed minimum width, and the value to the right of the decimal point, would be the guarinteed maximum width for everything to the right of the decimal point, truncating anything that would have been longer on the right of the decimal point. But, the entire width would have a minimum guarinteed width based on whatever was left. Assuming defined behaviour.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, my take on the bolded part in my post is that using the following things in one go is "undefined":
    - 0 flag (to give you leading zeros)
    - .3 (precision)
    - f (to give you a float)

    Looking at chrismiceli's post (4th in the thread), they say that the sample code gives output of "000.000", however, when I compile/run it, I get "1000000.000". Assuming chrismiceli did actually compile/run the code to get the result, I guess we're seeing the affects of undefined behaviour. If this is the case, I'd be interested to know what compiler is being used, and what are the results when the following is used in the same program:
    printf("value = %07f\n", value);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok, based on what you both are saying, it would indeed be interesting if chrismiceli were to run:
    printf("value = %07f\n", value);

    If it prints properly, then we are interpreting the standard correctly, and if you want portable code, you can't specify a precision with %0:
    printf("value = %07.3f\n", value);

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    First, let me apologize for being a bit sloppy (less sleep and more diaper changes have been the order of the day for over a week now). I was trying to present a possibility for "How can this be achieved with floats? (if it's possible)". I may have erred, but it did seem to me possible.

    I'm a bit tired, but the responses thus far are thought-provoking. One thing that I find of interest is this.
    Quote Originally Posted by Hammer
    "Undefined" is the word missing from this thread.
    0 For d, i, o, u, x, X, e, E, f, g and G conversions, leading zeros (following any indication of sign or base) are used to pad to the field width; no space padding is performed. If the 0 and - flags both appear, the 0 flag will be ignored. For d, i, o, u, x and X conversions, if a precision is specified, the 0 flag will be ignored. For other conversions, the behavior is undefined.
    It isn't "d, i, o, u, x and X" or some "other" conversion, it is the f conversion.

    I believe C99 and certain other implementations fix some of the shortcomings with snprintf.

    Again, I'm a bit tired. But I am interested in more rested follow-ups.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 08:17 AM
  3. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM