Mercurial > hg > fxanalyse
comparison FXAnalyse.c @ 128:8a69c40f10b2
Fix thousands separators number formatting function.
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Mon, 16 Dec 2013 19:50:20 +0100 |
parents | 2479ad53982b |
children | 24db0c2d5219 |
comparison
equal
deleted
inserted
replaced
127:2479ad53982b | 128:8a69c40f10b2 |
---|---|
160 | 160 |
161 | 161 |
162 char * thousands(char *buffer, int size, char *fmt, double val) | 162 char * thousands(char *buffer, int size, char *fmt, double val) |
163 { | 163 { |
164 // compute how many separators we need | 164 // compute how many separators we need |
165 int nsep = (int)(log10(fabs(val)+1.0))/3; | 165 #pragma DisableFunctionRuntimeChecking log10 |
166 int nsep = log10(fabs(val)) / 3; | |
166 // format value | 167 // format value |
167 int len = snprintf(buffer, size, fmt, val); | 168 int len = snprintf(buffer, size, fmt, val); |
168 char *src = buffer + len + 1; | 169 char *src = buffer + len; |
169 char *dst = src + nsep; | 170 char *dst = src + nsep; |
170 // copy till decimal separator | 171 // copy till decimal separator |
171 while (*src != '.') | 172 while (*src != '.') |
172 *dst-- = *src--; | 173 *dst-- = *src--; |
173 // copy till the beginning inserting separators | 174 // the next char is the decimal separator |
174 int n = -1; | 175 int n = -1; |
175 while (1) { | 176 // copy till src and dst point to the same location |
177 while (src != dst) { | |
176 *dst-- = *src--; | 178 *dst-- = *src--; |
179 // insert separator | |
177 if (isdigit(*src) && (++n) && ((n % 3) == 0)) | 180 if (isdigit(*src) && (++n) && ((n % 3) == 0)) |
178 *dst-- = ' '; | 181 *dst-- = ' '; |
179 if (src == buffer) | |
180 break; | |
181 } | 182 } |
182 return buffer; | 183 return buffer; |
183 } | 184 } |
184 | 185 |
185 | 186 |