Mercurial > hg > fxanalyse
comparison 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 |
comparison
equal
deleted
inserted
replaced
126:5bba1d1c3b46 | 127:2479ad53982b |
---|---|
157 static int CalcNPanel; | 157 static int CalcNPanel; |
158 static int EstimateN3Panel; | 158 static int EstimateN3Panel; |
159 static int LoggingPanel; | 159 static int LoggingPanel; |
160 | 160 |
161 | 161 |
162 // format and add thousands separator */ | 162 char * thousands(char *buffer, int size, char *fmt, double val) |
163 char * thousands(char *buffer, int len, char *fmt, double val) | 163 { |
164 { | 164 // compute how many separators we need |
165 int c = snprintf(buffer, len, fmt, val); | 165 int nsep = (int)(log10(fabs(val)+1.0))/3; |
166 char *p = strchr(buffer, '.'); | 166 // format value |
167 int n = buffer + c - p; | 167 int len = snprintf(buffer, size, fmt, val); |
168 while (p > (buffer + 3)) { | 168 char *src = buffer + len + 1; |
169 // shift by 3 digits | 169 char *dst = src + nsep; |
170 p -= 3; | 170 // copy till decimal separator |
171 n += 4; | 171 while (*src != '.') |
172 // make space for separator | 172 *dst-- = *src--; |
173 memmove(p + 1, p, n); | 173 // copy till the beginning inserting separators |
174 // insert thousand separator | 174 int n = -1; |
175 *p = ' '; | 175 while (1) { |
176 *dst-- = *src--; | |
177 if (isdigit(*src) && (++n) && ((n % 3) == 0)) | |
178 *dst-- = ' '; | |
179 if (src == buffer) | |
180 break; | |
176 } | 181 } |
177 return buffer; | 182 return buffer; |
178 } | 183 } |
179 | 184 |
180 | 185 |