]> granicus.if.org Git - php/commitdiff
- MFH Fixed Bug #29538 number_format and problem with 0
authorMarcus Boerger <helly@php.net>
Sun, 16 Jul 2006 10:53:55 +0000 (10:53 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 16 Jul 2006 10:53:55 +0000 (10:53 +0000)
NEWS
ext/standard/math.c
ext/standard/tests/strings/bug29538.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 2f235cbb40216c5ab4695a400b85436aba493e9f..808c5a5ecd0e8e7fedeb4d904d39523a94935b3a 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,7 @@ PHP 4                                                                      NEWS
 - Fixed bug #37720 (merge_php_config scrambles values). (Mike,
   pumuckel at metropolis dot de)
 - Fixed bug #37569 (WDDX incorrectly encodes high-ascii characters). (Ilia)
+- Fixed bug #29538 (number_format and problem with 0). (Matthew Wilmas)
 
 21 May 2006, Version 4.4.3RC1
 - Added control character checks for cURL extension's open_basedir/safe_mode
index 259ba17b1100e782af018f84e28f1de010f2f82d..8283d3a83ceb4d85ffce4f79c0330b39f9cec612 100644 (file)
@@ -1015,13 +1015,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);
@@ -1030,8 +1025,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 {
@@ -1047,7 +1044,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 */
@@ -1064,29 +1065,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 (file)
index 0000000..6af25fb
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+Bug #29538 (number_format and problem with 0)
+--FILE--
+<?php
+       echo number_format(0.25, 2, '', ''), "\n";
+       echo number_format(1234, 2, '', ',');
+?>
+--EXPECT--
+025
+1,23400