Commas in numbers

From: Rasdan (rasdan@PEAK.ORG)
Date: 04/19/98


Hey all,

  Here's a little function I whipped up tonight to place commas in an
integer as they would be written out longhand (or periods for people not
from the US).

It may not be done in the best way possible, but it works. If any of you
can help with the optimization I would much appreciate it. :)

----
Add to utils.h

char *place_commas(int num);

/* This just makes less typing, don't add if you don't want to use it */
#define COMMA(num)      (place_commas(num))

Add to bottom of utils.c:


/* Change if you don't wish commas */
#define SEPARATOR       ','

char *place_commas(int num)
{
  int i, j, k;

  char temp[50];

  *buf1 = '\0';
  *buf2 = '\0';

  sprintf(temp, "%d", num);

  i = strlen(temp);

  k = -1;

  for (j = i; j >= 0; j--) {
    k++;

    if (k > 3) {
      sprintf(buf1, "%c%c%s", temp[j], SEPARATOR, buf2);
      k = 1;
    } else
      sprintf(buf1, "%c%s", temp[j], buf2);
    sprintf(buf2, "%s", buf1);
  }
  return (buf2);
}


-------------
Using it:

ACMD(do_exp)
{
  sprintf(buf, "You current have %s experience points.\r\n",
        COMMA(GET_EXP(ch));
  send_to_char(buf, ch);
}

Let me know if you decide to use this, I'm just curious how many people
actually care :)

Rasdan

                                  /   \
        _                 )      ((   ))     (
       (@)               /|\      ))_((     /|\                 _
       |-|`\            / | \    (/\|/\)   / | \               (@)
       | | ------------/--|-voV---\`|'/--Vov-|--\--------------|-|
       |-|                  '^`   (o o)  '^`                   | |
       | |  James C. Thomas Jr.   `\Y/'  Founder of Finality   |-|
       |-|            rasdan@finality.drachen.com              | |
       | |             finality.drachen.com 4000               |-|
       |_|_____________________________________________________| |
       (@)       l   /\ /         ( (       \ /\   l         `\|-|
                 l /   V           \ \       V   \ l           (@)
                 l/                _) )_          \I
                                   `\ /'
                                     `


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST