From: Marcus Boerger Date: Sun, 16 Jul 2006 10:50:58 +0000 (+0000) Subject: - MFH Fixed Bug #29538 number_format and problem with 0 X-Git-Tag: php-5.2.0RC1~99 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dc571fa9e2721796fa749edb39fc1a1e1da81c3a;p=php - MFH Fixed Bug #29538 number_format and problem with 0 --- diff --git a/NEWS b/NEWS index 1c5b207ba5..058630ec5b 100644 --- a/NEWS +++ b/NEWS @@ -192,6 +192,7 @@ PHP NEWS stream context options). (Mike) - Fixed bug #34005 (oci_password_change() fails). (pholdaway at technocom-wireless dot com, Tony) +- Fixed bug #29538 (number_format and problem with 0). (Matthew Wilmas) 04 May 2006, PHP 5.1.4 - Added "capture_peer_cert" and "capture_peer_cert_chain" context options diff --git a/ext/standard/math.c b/ext/standard/math.c index ca3d4cccd0..0cea73ac79 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -976,13 +976,8 @@ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char tho is_negative = 1; d = -d; } - if (!dec_point && dec > 0) { - d *= pow(10, dec); - dec = 0; - } else { - dec = MAX(0, dec); - } + dec = MAX(0, dec); PHP_ROUND_WITH_FUZZ(d, dec); tmplen = spprintf(&tmpbuf, 0, "%.*f", dec, d); @@ -991,8 +986,10 @@ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char tho return tmpbuf; } + /* find decimal point, if expected */ + dp = dec ? strchr(tmpbuf, '.') : NULL; + /* calculate the length of the return buffer */ - dp = strchr(tmpbuf, '.'); if (dp) { integral = dp - tmpbuf; } else { @@ -1008,7 +1005,11 @@ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char tho reslen = integral; if (dec) { - reslen += 1 + dec; + reslen += dec; + + if (dec_point) { + reslen++; + } } /* add a byte for minus sign */ @@ -1025,29 +1026,29 @@ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char tho * Take care, as the sprintf implementation may return less places than * we requested due to internal buffer limitations */ if (dec) { - int declen = dp ? strlen(dp+1) : 0; - int topad = declen > 0 ? dec - declen : 0; + int declen = dp ? s - dp : 0; + int topad = dec > declen ? dec - declen : 0; /* pad with '0's */ - while (topad--) { *t-- = '0'; } if (dp) { - /* now copy the chars after the point */ - memcpy(t - declen + 1, dp + 1, declen); - + s -= declen + 1; /* +1 to skip the point */ t -= declen; - s -= declen; + + /* now copy the chars after the point */ + memcpy(t + 1, dp + 1, declen); } /* add decimal point */ - *t-- = dec_point; - s--; + if (dec_point) { + *t-- = dec_point; + } } - /* copy the numbers before the decimal place, adding thousand + /* copy the numbers before the decimal point, adding thousand * separator every three digits */ while(s >= tmpbuf) { *t-- = *s--; diff --git a/ext/standard/tests/strings/bug29538.phpt b/ext/standard/tests/strings/bug29538.phpt new file mode 100644 index 0000000000..6af25fb89a --- /dev/null +++ b/ext/standard/tests/strings/bug29538.phpt @@ -0,0 +1,10 @@ +--TEST-- +Bug #29538 (number_format and problem with 0) +--FILE-- + +--EXPECT-- +025 +1,23400