Welcome to the Builder Academy

Question Function to add commas to numbers for better readability

More
13 Sep 2025 17:10 #10893 by Salty
Greetings,

This is not my original work and the author is unknown, but I have modified it for my use case and to compile with tbaMUD v2018.  The function converts an input value (currently a long int) to a string with commas.  Useful especially for exp and gold values as they increase in the endgame.
Code:
char *add_commas(long long num) { #define DIGITS_PER_GROUP 3 #define BUFFER_COUNT 20 #define DIGITS_PER_BUFFER 32   int i, j, len, negative = (num < 0);   char num_string[DIGITS_PER_BUFFER];   static char comma_string[BUFFER_COUNT][DIGITS_PER_BUFFER];   static int which = 0;   sprintf(num_string, "%lli", num);   len = strlen(num_string);   for (i = j = 0; num_string[i]; ++i)   {     if ((len - i) % DIGITS_PER_GROUP == 0 && i && i - negative)       comma_string[which][j++] = ',';     comma_string[which][j++] = num_string[i];   }   comma_string[which][j] = '\0';   i = which;   which = (which + 1) % BUFFER_COUNT;   return comma_string[i]; #undef DIGITS_PER_GROUP #undef BUFFER_COUNT #undef DIGITS_PER_BUFFER }

Converting it to an 4bit int input is trivial so I leave that to you.

You will have to change your output to characters to a string instead of an int to avoid breaking your game.

Example:
Code:
Level   1 requires:        10,000 experience,          10,000 total experience Level   2 requires:        20,000 experience,          30,000 total experience Level   3 requires:        30,000 experience,          60,000 total experience Level   4 requires:        40,000 experience,         100,000 total experience Level   5 requires:        50,000 experience,         150,000 total experience Level   6 requires:        60,000 experience,         210,000 total experience Level   7 requires:        70,000 experience,         280,000 total experience Level   8 requires:        80,000 experience,         360,000 total experience Level   9 requires:        90,000 experience,         450,000 total experience Level  10 requires:       200,000 experience,         650,000 total experience Level  11 requires:       220,000 experience,         870,000 total experience Level  12 requires:       240,000 experience,       1,110,000 total experience Level  13 requires:       260,000 experience,       1,370,000 total experience Level  14 requires:       280,000 experience,       1,650,000 total experience Level  15 requires:       300,000 experience,       1,950,000 total experience Level  16 requires:       320,000 experience,       2,270,000 total experience Level  17 requires:       340,000 experience,       2,610,000 total experience Level  18 requires:       360,000 experience,       2,970,000 total experience Level  19 requires:       380,000 experience,       3,350,000 total experience Level  20 requires:       600,000 experience,       3,950,000 total experience Level  21 requires:       630,000 experience,       4,580,000 total experience

Have fun,

Salty

Please Log in or Create an account to join the conversation.

Time to create page: 0.174 seconds