Any easy to printf an interger in 9,999, 99 format?

Discussion in 'C Programming' started by I_have_nothing, May 13, 2005.

  1. Hi!

    Is there any easy way to printf an integer in a way like
    1,234,567?
    I know "%d" can be usd to print it as 1234567.
    Any type field in format specification can do that?
    Or any easy way to do that?

    How about to do that same thing in float type?
    such as make 987564321.85 into 987,564,321.85 ?

    Thanks!
     
    I_have_nothing, May 13, 2005
    #1
    1. Advertising

  2. Re: Any easy to printf an interger in 9,999, 99 format?

    Sorry. The title should be "... in 9,999,999 format".
     
    I_have_nothing, May 13, 2005
    #2
    1. Advertising

  3. I_have_nothing

    Richard Bos Guest

    "I_have_nothing" <> wrote:

    > Is there any easy way to printf an integer in a way like
    > 1,234,567?


    Short of formatting it yourself, no. And if only you'd read the FAQ, as
    a good netizen does... <http://www.eskimo.com/~scs/C-faq/q12.11.html>.

    Richard
     
    Richard Bos, May 13, 2005
    #3
  4. I_have_nothing

    CBFalconer Guest

    I_have_nothing wrote:
    >
    > Is there any easy way to printf an integer in a way like 1,234,567?
    > I know "%d" can be usd to print it as 1234567.
    > Any type field in format specification can do that?
    > Or any easy way to do that?
    >
    > How about to do that same thing in float type?
    > such as make 987564321.85 into 987,564,321.85 ?


    Try this. Compile with TESTING defined for a demo driver.

    #ifndef putnums_h_ /* --- file putnums.h --- */
    # define putnums_h_

    # ifdef __cplusplus
    extern "C" {
    # endif

    /* Using binary as an example, code to ourput numbers
    in a field, while injecting commas at intervals.

    By C.B. Falconer. Put in public domain. 2003-02-15
    */

    /* ------------------- */

    /* Negative field for left justification, 0 based */
    /* clump is interval for group separator, 0 for none */
    int putnum(FILE *fp, long v, int base,
    int field, int clump);

    /* ------------------- */

    /* Negative field for left justification, 0 based */
    /* clump is interval for group separator, 0 for none */
    int putunum(FILE *fp, unsigned long v, int base,
    int field, int clump);

    /* Macros to ease use for decimal output */
    #define putdnum(fp, v, field, clump) \
    putnum(fp, v, 10, field, clump)
    #define putudnum(fp, v, field, clump) \
    putunum(fp, v, 10, field, clump)

    # ifdef __cplusplus
    }
    # endif
    #endif
    /* --- end putnums.h --- */

    /* --- file putnums.c ---

    Using binary as an example, code to ourput numbers
    in a field, while injecting commas at intervals.

    By C.B. Falconer. Put in public domain. 2003-02-15
    */
    #include <stdio.h>
    #include "putnums.h"

    #ifdef TESTING /* Add in a demonstration driver */
    # include <limits.h>

    # define BASE 10 /* Try 2 through 16 here only */
    # define GROUP 3 /* with 0, 4 or 3 here */
    #endif

    /* ------------------- */

    /* The original call must pass in depth == 0 */
    /* field is zero based, so 36 allows 37 chars */
    static int putval(FILE *fp, unsigned long v, int base,
    int field, int clump, int neg,
    int depth)
    {
    int retval;
    static char hexchars[16] = "0123456789abcdef";

    if (depth && clump && ((depth % clump) == 0)) field--;
    if ((v / base) > 0) {
    retval = 1 + putval(fp, v/base, base, field,
    clump, neg, depth+1);
    }
    else {
    if (neg) field--;
    while (field > depth) {
    putc(' ', fp);
    field--;
    }
    if (neg) {
    putc('-', fp);
    retval = 2;
    }
    else retval = 1;
    }
    /* Revise this for base value larger than 16 */
    putc((v % base)[hexchars], fp);

    if (depth && clump && ((depth % clump) == 0)) {
    putc(',', fp);
    retval++;
    }
    return retval;
    } /* putval */

    /* ------------------- */

    /* Negative field for left justification, 0 based */
    /* clump is interval for group separator, 0 for none */
    int putnum(FILE *fp, long v, int base,
    int field, int clump)
    {
    int retval;

    if (v < 0) retval = putval(fp, -v, base, field, clump, 1, 0);
    else retval = putval(fp, v, base, field, clump, 0, 0);
    while ((field + retval) <= 0) {
    field++;
    putc(' ', fp);
    }
    return retval;
    } /* putnum */

    /* ------------------- */

    /* Negative field for left justification, 0 based */
    /* clump is interval for group separator, 0 for none */
    int putunum(FILE *fp, unsigned long v, int base,
    int field, int clump)
    {
    int retval;

    retval = putval(fp, v, base, field, clump, 0, 0);
    while ((field + retval) <= 0) {
    field++;
    putc(' ', fp);
    }
    return retval;
    } /* putunum */

    /* ------------------- */

    #ifdef TESTING
    int main(void)
    {
    int i, lgh;

    for (i = 0; i < 50; i++) putchar('0' + i % 10);
    putchar('\n');
    for (i = 0; i < 12; i++) {
    lgh = putnum(stdout, i, BASE, 36, GROUP);
    putchar(' ');
    lgh = putnum(stdout, lgh, BASE, 8, GROUP);
    puts(".");
    }
    i = INT_MAX - 4;
    do {
    i++;
    lgh = putnum(stdout, i, BASE, 36, GROUP);
    putchar(' ');
    lgh = putnum(stdout, lgh, BASE, 8, GROUP);
    puts(".");
    } while (i < INT_MAX);

    i = INT_MIN + 4;
    do {
    i--;
    lgh = putnum(stdout, i, BASE, 36, GROUP);
    putchar(' ');
    lgh = putnum(stdout, lgh, BASE, 8, GROUP);
    puts(".");
    lgh = putunum(stdout, (unsigned long)i, BASE, 36, 0);
    putchar(' ');
    lgh = putnum(stdout, lgh, BASE, 8, GROUP);
    puts(".");
    } while (i > INT_MIN);

    lgh = putunum(stdout, 1, BASE, -36, GROUP);
    putchar(' ');
    lgh = putunum(stdout, lgh, BASE, 8, GROUP);
    puts(".");

    for (i = 0; i < 4; i++) {
    lgh = putudnum(stdout, (unsigned long)-i, 36, GROUP);
    putchar(' ');
    lgh = putdnum(stdout, lgh, 8, GROUP);
    puts(".");
    lgh = putunum(stdout, (unsigned long)-i, 16, 36, 4);
    putchar(' ');
    lgh = putunum(stdout, lgh, BASE, -8, GROUP);
    puts(".");
    lgh = putunum(stdout, (unsigned long)-i, 2, -36, 4);
    putchar(' ');
    lgh = putunum(stdout, lgh, BASE, 8, GROUP);
    puts(".");
    }
    return 0;
    } /* main */
    #endif
    /* --- end putnums.c --- */

    --
    "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
     
    CBFalconer, May 13, 2005
    #4
  5. Re: Any easy to printf an interger in 9,999, 99 format?

    "I_have_nothing" <> writes:

    >Is there any easy way to printf an integer in a way like
    >1,234,567?
    >I know "%d" can be usd to print it as 1234567.
    >Any type field in format specification can do that?
    >Or any easy way to do that?


    >How about to do that same thing in float type?
    >such as make 987564321.85 into 987,564,321.85 ?



    An off-topic, non-locale-ized function for integers.
    You'll need to modify it for floating point:

    --
    Chris.


    #define N_COMMABUFS 4

    char *format64(int64_t value)
    {
    static char result[N_COMMABUFS][32];
    static int whichbuf = 0;

    char *rp = &result[whichbuf][0];

    whichbuf = (whichbuf+1) % N_COMMABUFS;

    sprintf(rp, "%lld", value);
    if(output_commas) {
    char buf[32], *b=buf, *r=rp;
    int i, len;

    if(rp[0] == '-') {
    *b++ = '-';
    ++r;
    }

    len = strlen(r);
    for(i=0 ; i<len ; ++i) {
    *b++ = *r++;
    if(*r && ((len-i)%3) == 1)
    *b++ = ',';
    }
    *b = '\0';
    strcpy(rp, buf);
    }
    return(rp);
    }
     
    Chris McDonald, May 13, 2005
    #5
    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. =?Utf-8?B?VGltOjouLg==?=

    Cannot be converted to 'Interger'

    =?Utf-8?B?VGltOjouLg==?=, Nov 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,682
    chanmmn
    Nov 3, 2004
  2. Buddy Ackerman

    999 error

    Buddy Ackerman, Mar 3, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,434
    David Wang [Msft]
    Mar 4, 2006
  3. Michael B Allen

    Random Integers from 0 to 999

    Michael B Allen, Mar 24, 2005, in forum: C Programming
    Replies:
    36
    Views:
    1,097
    Grumble
    Apr 20, 2005
  4. HAPPY NEW YEAR Funny Christmas wallpapers, theme F

    Funny Christmas wallpapers, theme & 15,999$/- per day

    HAPPY NEW YEAR Funny Christmas wallpapers, theme F, Dec 26, 2008, in forum: C Programming
    Replies:
    0
    Views:
    305
    HAPPY NEW YEAR Funny Christmas wallpapers, theme F
    Dec 26, 2008
  5. Armin Gajda

    Regular Expression for 001-999

    Armin Gajda, Feb 7, 2006, in forum: Perl Misc
    Replies:
    13
    Views:
    882
    William James
    Feb 7, 2006
Loading...

Share This Page