diff options
author | goodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2006-11-14 15:44:19 +0000 |
---|---|---|
committer | goodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2006-11-14 15:44:19 +0000 |
commit | 383d56f1070d1e299d28b43a7782d0c8fbf048a5 (patch) | |
tree | d63ebf9526932a59e07b76ee19576d7ccc28c446 /src/util | |
parent | 7ac774cd685ae940aaecbba9d67eabc4b5feb85d (diff) |
Fixed formatting for the %g format conversion for small numbers.
Fixes PR 2058.
git-svn-id: http://svn.cactuscode.org/flesh/trunk@4386 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/snprintf.c | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/src/util/snprintf.c b/src/util/snprintf.c index 6b4b5bb0..6ae9e24d 100644 --- a/src/util/snprintf.c +++ b/src/util/snprintf.c @@ -66,6 +66,9 @@ * Added support for 'e' format. * Added support for 'g' format. * + * Tom Goodale <goodale@cct.lsu.edu> 2006-11-14 + * Fixed support for 'g' format for small numbers. + * **************************************************************/ @@ -648,7 +651,7 @@ static int fmtfp (char *buffer, size_t *currlen, size_t maxlen, exponent = (long)logvalue; - if(flags & DP_F_G && exponent >= -4 && exponent < max) + if(fvalue==0 || (flags & DP_F_G && exponent >= -4 && exponent < max)) { /* With 'G' format use 'f' if exponent is small enough. */ exponent = 0; @@ -703,19 +706,37 @@ static int fmtfp (char *buffer, size_t *currlen, size_t maxlen, iconvert[iplace] = 0; /* Convert fractional part */ - do { - fconvert[fplace++] = - (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10]; - fracpart = (fracpart / 10); - } while(fracpart && (fplace < 20)); - /* Make sure we replace any leading zeros. */ - while(fplace < max) + + /* Check for special case of a 'g' conversion with no fractional part */ + if(flags & DP_F_G && fracpart == 0) + { + fplace = 0; + } + else { - fconvert[fplace++] = '0'; + do { + if((flags & DP_F_G) && fracpart % 10 == 0) + { + max--; + } + else + { + fconvert[fplace++] = + (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10]; + } + fracpart = (fracpart / 10); + } while(fracpart && (fplace < 20)); + /* Make sure we replace any leading zeros. */ + while(fplace < max) + { + fconvert[fplace++] = '0'; + } + if (fplace == 20) fplace--; } - if (fplace == 20) fplace--; + fconvert[fplace] = 0; + if(flags & DP_F_EXP) { /* Convert exponent part */ @@ -739,10 +760,10 @@ static int fmtfp (char *buffer, size_t *currlen, size_t maxlen, eplace = 0; } - /* -1 for decimal point, another -1 if we are printing a sign */ - padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0) - eplace; + /* -1 for decimal point if there, another -1 if we are printing a sign */ + padlen = min - iplace - max - (((flags & DP_F_G) && fplace == 0) ? 0 : 1) - ((signvalue) ? 1 : 0) - eplace; zpadlen = max - fplace; - if (zpadlen < 0) + if (zpadlen < 0 || (flags & DP_F_G)) zpadlen = 0; if (padlen < 0) padlen = 0; @@ -778,7 +799,7 @@ static int fmtfp (char *buffer, size_t *currlen, size_t maxlen, * Decimal point. This should probably use locale to find the correct * char to print out. */ - if (max > 0) + if (max > 0 && ! ((flags & DP_F_G) && fplace == 0)) { total += dopr_outch (buffer, currlen, maxlen, '.'); |