"comma delimited" numeric output from printf()???

Discussion in 'C Programming' started by jchludzinski@gmail.com, Jun 1, 2005.

  1. Guest

    Is there a format specification for printf that will result in: 1000000
    being printed as 1,000,000? Or 1000000.0 as 1,000,000.0?

    ---John
     
    , Jun 1, 2005
    #1
    1. Advertising

  2. wrote:
    > Is there a format specification for printf that will result in: 1000000
    > being printed as 1,000,000? Or 1000000.0 as 1,000,000.0?
    >
    > ---John


    See Question 12.11 on the FAQ, it was posted today and can be found at
    http://www.eskimo.com/~scs/C-faq/top.html.

    Robert Gamble
     
    Robert Gamble, Jun 1, 2005
    #2
    1. Advertising

  3. Guest

    Oh well, I must admit I was hopin' for another answer! ---John
     
    , Jun 1, 2005
    #3
  4. What were you hoping for? A format modifier that throws
    internationalization to the wind and adds commas to a number in just
    the way you need it?
     
    James Daughtry, Jun 1, 2005
    #4
  5. Guest

    Yes! ---John
     
    , Jun 1, 2005
    #5
  6. Unfortunately, the answer to any question is more likely to be a lot
    harder than you hope for. But that's life, and C doesn't make life
    easy, but it's a whole mess of fun. ;-)
     
    James Daughtry, Jun 1, 2005
    #6
  7. On 1 Jun 2005 14:19:21 -0700, in comp.lang.c ,
    "" <> wrote:

    >Yes! ---John


    Firstly, please quote enough of the message you're replying to, for
    the reply to make sense. A good start is not to use google's broken
    'reply' button

    Secondly, if you want some locale-specific way of printing numbers,
    read your OS's function reference manual. Many provide one.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Jun 1, 2005
    #7
  8. CBFalconer Guest

    Robert Gamble wrote:
    > wrote:
    >
    >> Is there a format specification for printf that will result in:
    >> 1000000 being printed as 1,000,000? Or 1000000.0 as 1,000,000.0?

    >
    > See Question 12.11 on the FAQ, it was posted today and can be found
    > at http://www.eskimo.com/~scs/C-faq/top.html.


    You may also use my coding:

    #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 --- */

    --
    "I conclude that there are two ways of constructing a software
    design: One way is to make it so simple that there are obviously
    no deficiencies and the other way is to make it so complicated
    that there are no obvious deficiencies." -- C. A. R. Hoare
     
    CBFalconer, Jun 1, 2005
    #8
  9. wrote:

    > Is there a format specification for printf that will result in: 1000000
    > being printed as 1,000,000? Or 1000000.0 as 1,000,000.0?


    It seems to me that C borrowed much of its formatting from Fortran,
    which doesn't include comma formatting. (Except for the behavior when a
    number is too big for the specified width.)

    The PL/I P format though, I believe, will do it. As I understand it
    the P (picture) formatting was borrowed from COBOL, where comma
    formatting is more common. For scientific computing it is considered a
    waste of precious paper.

    -- glen
     
    glen herrmannsfeldt, Jun 3, 2005
    #9
    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. Yama

    Comma Delimited

    Yama, Dec 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    409
    Ken Cox [Microsoft MVP]
    Dec 16, 2003
  2. Luis Esteban Valencia

    Read Comma Delimited File

    Luis Esteban Valencia, Jul 27, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,824
    Paul Clement
    Jul 27, 2005
  3. Edward A Thompson

    JavaMail MimeMessage - comma delimited?

    Edward A Thompson, Oct 8, 2003, in forum: Java
    Replies:
    9
    Views:
    5,708
    GaryM
    Oct 10, 2003
  4. ouioui
    Replies:
    1
    Views:
    1,666
    Mukul Gandhi
    Sep 23, 2004
  5. samuels
    Replies:
    3
    Views:
    382
    samuels
    Jul 18, 2005
Loading...

Share This Page