]> granicus.if.org Git - php/commitdiff
Ensure number_format() doesn't include sign for zero
authorCraig Duncan <git@duncanc.co.uk>
Thu, 24 Nov 2016 21:56:53 +0000 (21:56 +0000)
committerJoe Watkins <krakjoe@php.net>
Tue, 9 May 2017 09:17:19 +0000 (10:17 +0100)
UPGRADING
ext/standard/math.c
ext/standard/tests/math/number_format_negative_zero.phpt [new file with mode: 0644]

index 78ce94cedcda0e7947a4becf5a0985bdffacf714..0530958def0260894f85fea47999778408c1f401 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -187,6 +187,7 @@ See also: https://wiki.php.net/rfc/deprecations_php_7_2
   . count() now raises a warning when an invalid parameter is passed.
     Only arrays and objects implementing the Countable interface should be passed.
   . pack() and unpack() now support float and double in both little and big endian.
+  . number_format() ensures zero values never contain a negative sign.
 
 - XML:
   . utf8_encode() and utf8_decode() have been moved to the Standard extension
index 169d0a0b1dfeb3522a6bc417761b33d59cbde943..794128be6c51eaac2f1eec0670725b98821a35c9 100644 (file)
@@ -1143,6 +1143,11 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, char *dec_poin
                return tmpbuf;
        }
 
+       /* Check if the number is no longer negative after rounding */
+       if (is_negative && d == 0) {
+               is_negative = 0;
+       }
+
        /* find decimal point, if expected */
        if (dec) {
                dp = strpbrk(ZSTR_VAL(tmpbuf), ".,");
diff --git a/ext/standard/tests/math/number_format_negative_zero.phpt b/ext/standard/tests/math/number_format_negative_zero.phpt
new file mode 100644 (file)
index 0000000..743a535
--- /dev/null
@@ -0,0 +1,16 @@
+--TEST--
+Prevent number_format from returning negative zero
+--FILE--
+<?php
+
+$number = -1.15E-15;
+
+var_dump($number);
+var_dump(number_format($number, 2));
+var_dump(number_format(-0.01, 2));
+
+?>
+--EXPECT--
+float(-1.15E-15)
+string(4) "0.00"
+string(5) "-0.01"