printf, padding integral double values

Discussion in 'C Programming' started by Daniel Molina Wegener, Feb 14, 2005.

  1. Hello,

    How can I pad integral values on double variables with
    printf.

    I know that %.nf or %.ng sets the precision. But I want to
    pad the integral value with zeroes, like 001.023 or 032.012.

    Any way to do this with printf?.

    Regards.
    --
    . 0 . | Daniel Molina Wegener
    . . 0 | dmw at trauco dot cl
    0 0 0 | http://trauco.cl/
    Daniel Molina Wegener, Feb 14, 2005
    #1
    1. Advertising

  2. Daniel Molina Wegener

    Michael Mair Guest

    Daniel Molina Wegener wrote:
    > Hello,
    >
    > How can I pad integral values on double variables with
    > printf.
    >
    > I know that %.nf or %.ng sets the precision. But I want to
    > pad the integral value with zeroes, like 001.023 or 032.012.
    >
    > Any way to do this with printf?.
    >
    > Regards.


    #include <stdio.h>

    int main (void)
    {
    printf("%0*.*f\n", 6+1, 3, 1.23);
    return 0;
    }

    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.
    Michael Mair, Feb 14, 2005
    #2
    1. Advertising

  3. Daniel Molina Wegener wrote:
    > Hello,
    >
    > How can I pad integral values on double variables with
    > printf.
    >
    > I know that %.nf or %.ng sets the precision. But I want to
    > pad the integral value with zeroes, like 001.023 or 032.012.
    >
    > Any way to do this with printf?.


    #include <stdio.h>

    int main(void)
    {
    double x[] = { 1.023, 32.012 };
    size_t i;

    printf("[output]\n");
    for (i = 0; i < sizeof x / sizeof *x; i++)
    printf("%%g: %g, %%f %f, %%.3f %.3f, %%07.3f %07.3f\n",
    x, x, x, x);
    return 0;
    }

    [output]
    %g: 1.023, %f 1.023000, %.3f 1.023, %07.3f 001.023
    %g: 32.012, %f 32.012000, %.3f 32.012, %07.3f 032.012


    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    Martin Ambuhl, Feb 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Prinke
    Replies:
    0
    Views:
    325
    Michal Prinke
    Oct 19, 2004
  2. Piotr B.
    Replies:
    2
    Views:
    506
    CBFalconer
    Oct 19, 2004
  3. Sydex
    Replies:
    12
    Views:
    6,445
    Victor Bazarov
    Feb 17, 2005
  4. Michal Prinke

    printf() formatting - stripping zeroes, padding

    Michal Prinke, Oct 19, 2004, in forum: C Programming
    Replies:
    0
    Views:
    323
    Michal Prinke
    Oct 19, 2004
  5. Mihai Rusu
    Replies:
    1
    Views:
    303
    James Kanze
    Aug 8, 2008
Loading...

Share This Page