Thread: Number WITH appropriate comma's

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    Number WITH appropriate comma's

    I've just finished coding up a little with names and points:
    Code:
    Big Al        176
    Charlie     1,850
    Skittles    4,004
    All went well, UNTIL I wanted to print out the integers WITH right justification AND comma's.

    Since the numbers were "picked" out of several other string fields in each record, I put them into a string - so far, no comma's.

    Then I had to change them into a number, so I used atoi to do that. So far, so good.

    Finished my calculations with the numbers, and a new number is loaded into that string array, using sprintf(string, ...). Still, no comma's!

    Now it's time to print it all out, but printf doesn't have a "print it with comma's" format specifier.

    So NOW the fun begins

    I got something put together that adds the padding and prints the comma's OK, but I'm wondering what would be a "standard" way to handle this?

    Please, don't say "google", because I've googled this till the cows came home, already.

    Thanks in advance. Oddly, my books make no mention of doing this. It's like nobody ever needed it, or something!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Thanks in advance. Oddly, my books make no mention of doing this. It's like nobody ever needed it, or something!

    Well, it's an idea, but it only adds complexity to otherwise simple programs.
    You would have to have a big enough buffer ready to do this, but

    Convert a number to a string as you normally would, but use a big string.
    Starting from the rightmost place before the decimal point, shift three digits to the right.
    And slide in a comma in the newly vacated space.
    Continue until you reach the most significant tens place in the string.
    Optionally, resize the string to something reasonable, and return the string.

    To do this without headaches you'll need a bit of room in your strings, but it can't be too hard; especially if you're working with integers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Locale, huh? Never would have searched for that. Thanks, Salem.

    For those who are "locale-challenged", this is a C way of doing that right justified and with comma's, number printout.

    In this code snippet, sprintf is first putting the result of the computations back into a string called "folder[1].strpoints" (which is in the struct called folder).

    Code:
                sprintf(folder[1].strpoints, "%ld", gainpts); /*load average #*/
                length = strlen(folder[1].strpoints); /*into string & get it's*/ 
                                                     /*length for a pretty print*/
                commnum = length / 4;              /* commnum  holds the number of comma's  */
    
                /* adds the padded spaces for right justified numbers */
                for (j = 0; j < 12 - (length + commnum); j++)  {
                   printf(" ");
                   //fprintf(out, " ");
                }
                
                /* digit is printed out first, then the comma, if needed */
                for (n = 0, o = length; n < length; n++) { 
                   printf(out, "%c", folder[1].strpoints[n]);
                   o--;
                   if (folder[1].strpoints[n] != ' ' && n % 3 == 0 && n > 0)  { /*This is IT! %)  */ 
                      printf("%c", ',');
                      //fprintf(out, "%c", ',');
                   }
                }
    Probably more work to do that than it was worth, but it did look nice.
    Last edited by Adak; 06-16-2007 at 11:38 AM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    sprintf returns the number of bytes stored in buffer, not counting the terminating null character.
    So why do you need to call strlen afterwards? check the return value of sprintf directly

    Your /4 a little bothers me

    1300600 - length is 7 result is 1, but should be 2... Are you sure about your formula?

    And I hate var named o.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by vart View Post
    So why do you need to call strlen afterwards? check the return value of sprintf directly

    Your /4 a little bothers me

    1300600 - length is 7 result is 1, but should be 2... Are you sure about your formula?

    And I hate var named o.
    1) strlen() was already a part of my code, so I just left it in there. I don't have much experience with sprintf().

    2) Yeah, /4 seems intuitively wrong, doesn't it?

    Works fine in tests up to 5 digits.

    This code was taken from a large computational function with LOTS of looping variables already in use. Yes, I hate it also - too much like a zero.

    I would change it for the post, except that it's rather "fragile", and then I couldn't easily be sure it would work, properly. As is, it's run through several thousands of records, and handles it correctly. (but only up to 5 digits).

    Thanks for the comments and idea's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM