Pad leading zeroes for float in printf

This is a discussion on Pad leading zeroes for float in printf within the C Programming forums, part of the General Programming Boards category; Hello, I have the following code to print the value of a float with leading/padded zero's. My desired output is: ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Pad leading zeroes for float in printf

    Hello,

    I have the following code to print the value of a float with leading/padded zero's.

    My desired output is:
    PRINT - INTEGER : 01234 and FLOAT : 00123.00003456

    The current output upon executing the below code is:
    PRINT - INTEGER : 01234 and FLOAT : 123.34559631

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    int main()
    {
    
    int num_int = 1234;
    float num_flt = 123.3456;
    
    fprintf(stdout, " PRINT -  INTEGER : %05d and FLOAT : %05.08f \n", num_int, num_flt);
    
    return(0);
    }

    Can you please let me know how to format the float to yield the desired result. Should i convert it to a string to achieve this?
    Any suggestions/advice is deeply appreciated.

    Thank you.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    %05.8f should work.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    You do realize that 123.3456 is a different number from 00123.00003456, right? Those "captive" zeros between the decimal point and the 3456 actually change the value of the number. For example, $1.01 is one dollar and one cent. $1.1 is one dollar and ten cents. I would be very upset if every time I deposited $1.10, my bank only credited my account $1.01 and kept the other $.09.

    The printf family of functions doesn't provide any mechanism for changing the value of any data you give it, and for very good reason! Can I ask why you want to do this? It sounds more like you're confused as to what you want, rather than you just don't know how to make printf do what you want.

    That being said, you need to print this in two parts:
    Code:
    #include<math.h>
    ...
        whole = truncf(num_flt);
        fraction = (num_flt - whole) * 10000;  // The number of zeros here must be enough to move everything from the right of the decimal point to the left
        printf("FLOAT : %05.f.%08.f\n", whole, fraction);
    You may have to link with the math library separately for this to work. Also, you may find it easier to store the whole number and fraction portion as two separate integers (perhaps in a struct for convenience).

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    Thank you for your replies and pointing me in the right direction. I just wanted to pad leading zeros to the whole number only.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf a int/float pointer
    By jeanluca in forum C Programming
    Replies: 11
    Last Post: 04-04-2010, 09:26 AM
  2. C Double Float and printf problem
    By SlyVixsky in forum C Programming
    Replies: 31
    Last Post: 08-01-2009, 01:28 AM
  3. Printf float printing
    By judgex in forum C Programming
    Replies: 3
    Last Post: 12-08-2007, 01:47 AM
  4. printf: zero padding float
    By Laserve in forum C Programming
    Replies: 22
    Last Post: 06-16-2004, 06:08 PM
  5. Removing zeroes from float
    By lollerpoller in forum C Programming
    Replies: 1
    Last Post: 05-14-2004, 03:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21