lib: add function for formatting numbers w/ digit grouping

This commit is contained in:
David Anderson 2015-08-11 14:18:06 -07:00
parent e524670875
commit cf13cb256b
2 changed files with 32 additions and 0 deletions

View File

@ -756,3 +756,34 @@ vector<string> split(string s, char delim) {
} }
return result; return result;
} }
char *comma_print(unsigned long n) {
static char comma = 0;
static char retbuf[30];
char *p = &retbuf[sizeof(retbuf)-1];
int i = 0;
if (!comma) {
struct lconv *lcp = localeconv();
if (lcp) {
if (lcp->thousands_sep != NULL && *lcp->thousands_sep) {
comma = *lcp->thousands_sep;
} else {
comma = ',';
}
}
}
*p = 0;
do {
if (i%3 == 0 && i != 0) {
*--p = comma;
}
*--p = '0' + n % 10;
n /= 10;
i++;
} while (n);
return p;
}

View File

@ -101,4 +101,5 @@ extern const char* batch_state_string(int state);
extern void strip_translation(char* p); extern void strip_translation(char* p);
extern std::vector<std::string> split(std::string, char delim); extern std::vector<std::string> split(std::string, char delim);
extern char* comma_print(unsigned long);
#endif #endif