From: Craig Duncan Date: Thu, 24 Nov 2016 21:56:53 +0000 (+0000) Subject: Ensure number_format() doesn't include sign for zero X-Git-Tag: php-7.2.0alpha2~15^2~35 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e946d074dd6df1f99b844c84e7179eb09f11c602;p=php Ensure number_format() doesn't include sign for zero --- diff --git a/UPGRADING b/UPGRADING index 78ce94cedc..0530958def 100644 --- 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 diff --git a/ext/standard/math.c b/ext/standard/math.c index 169d0a0b1d..794128be6c 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -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 index 0000000000..743a5350e2 --- /dev/null +++ b/ext/standard/tests/math/number_format_negative_zero.phpt @@ -0,0 +1,16 @@ +--TEST-- +Prevent number_format from returning negative zero +--FILE-- + +--EXPECT-- +float(-1.15E-15) +string(4) "0.00" +string(5) "-0.01"