Mercurial > hg > fxanalyse
diff FXAnalyse.c @ 127:2479ad53982b
User iterface tweaks. Better thousands separators number formatting function.
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Fri, 13 Dec 2013 17:04:28 +0100 |
parents | 37535873ee83 |
children | 8a69c40f10b2 |
line wrap: on
line diff
--- a/FXAnalyse.c Fri Dec 13 17:03:36 2013 +0100 +++ b/FXAnalyse.c Fri Dec 13 17:04:28 2013 +0100 @@ -159,20 +159,25 @@ static int LoggingPanel; -// format and add thousands separator */ -char * thousands(char *buffer, int len, char *fmt, double val) +char * thousands(char *buffer, int size, char *fmt, double val) { - int c = snprintf(buffer, len, fmt, val); - char *p = strchr(buffer, '.'); - int n = buffer + c - p; - while (p > (buffer + 3)) { - // shift by 3 digits - p -= 3; - n += 4; - // make space for separator - memmove(p + 1, p, n); - // insert thousand separator - *p = ' '; + // compute how many separators we need + int nsep = (int)(log10(fabs(val)+1.0))/3; + // format value + int len = snprintf(buffer, size, fmt, val); + char *src = buffer + len + 1; + char *dst = src + nsep; + // copy till decimal separator + while (*src != '.') + *dst-- = *src--; + // copy till the beginning inserting separators + int n = -1; + while (1) { + *dst-- = *src--; + if (isdigit(*src) && (++n) && ((n % 3) == 0)) + *dst-- = ' '; + if (src == buffer) + break; } return buffer; }