Mercurial > hg > nginx
comparison src/core/ngx_string.c @ 4604:d3fc672fd6cc
Fix of "%f" format handling.
ngx_sprintf("%.2f", 0.999) incorrectly resulted in "0.100" instead of "1.00".
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 23 Apr 2012 11:11:32 +0000 |
parents | d620f497c50f |
children | 814541b2ec23 |
comparison
equal
deleted
inserted
replaced
4603:3fd9155f9534 | 4604:d3fc672fd6cc |
---|---|
144 u_char * | 144 u_char * |
145 ngx_vslprintf(u_char *buf, u_char *last, const char *fmt, va_list args) | 145 ngx_vslprintf(u_char *buf, u_char *last, const char *fmt, va_list args) |
146 { | 146 { |
147 u_char *p, zero; | 147 u_char *p, zero; |
148 int d; | 148 int d; |
149 double f, scale; | 149 double f; |
150 size_t len, slen; | 150 size_t len, slen; |
151 int64_t i64; | 151 int64_t i64; |
152 uint64_t ui64; | 152 uint64_t ui64, frac; |
153 ngx_msec_t ms; | 153 ngx_msec_t ms; |
154 ngx_uint_t width, sign, hex, max_width, frac_width, n; | 154 ngx_uint_t width, sign, hex, max_width, frac_width, scale, n; |
155 ngx_str_t *v; | 155 ngx_str_t *v; |
156 ngx_variable_value_t *vv; | 156 ngx_variable_value_t *vv; |
157 | 157 |
158 while (*fmt && buf < last) { | 158 while (*fmt && buf < last) { |
159 | 159 |
363 *buf++ = '-'; | 363 *buf++ = '-'; |
364 f = -f; | 364 f = -f; |
365 } | 365 } |
366 | 366 |
367 ui64 = (int64_t) f; | 367 ui64 = (int64_t) f; |
368 frac = 0; | |
369 | |
370 if (frac_width) { | |
371 | |
372 scale = 1; | |
373 for (n = frac_width; n; n--) { | |
374 scale *= 10; | |
375 } | |
376 | |
377 frac = (uint64_t) ((f - (double) ui64) * scale + 0.5); | |
378 | |
379 if (frac == scale) { | |
380 ui64++; | |
381 frac = 0; | |
382 } | |
383 } | |
368 | 384 |
369 buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width); | 385 buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width); |
370 | 386 |
371 if (frac_width) { | 387 if (frac_width) { |
372 | |
373 if (buf < last) { | 388 if (buf < last) { |
374 *buf++ = '.'; | 389 *buf++ = '.'; |
375 } | 390 } |
376 | 391 |
377 scale = 1.0; | 392 buf = ngx_sprintf_num(buf, last, frac, '0', 0, frac_width); |
378 | |
379 for (n = frac_width; n; n--) { | |
380 scale *= 10.0; | |
381 } | |
382 | |
383 /* | |
384 * (int64_t) cast is required for msvc6: | |
385 * it cannot convert uint64_t to double | |
386 */ | |
387 ui64 = (uint64_t) ((f - (int64_t) ui64) * scale + 0.5); | |
388 | |
389 buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width); | |
390 } | 393 } |
391 | 394 |
392 fmt++; | 395 fmt++; |
393 | 396 |
394 continue; | 397 continue; |