com php-src: Ensure number_format() doesn't include sign for zero: UPGRADING ext/standard/math.c e
xt/standard/tests/math/number_format_negative_zero.phpt
| From: | Joe Watkins | Date: | Tue, 09 May 2017 09:17:19 +0000 |
| Subject: | com php-src: Ensure number_format() doesn't include sign for zero: UPGRADING ext/standard/math.c e xt/standard/tests/math/number_format_negative_zero.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-97795@lists.php.net to get a copy of this message | ||
Commit: e946d074dd6df1f99b844c84e7179eb09f11c602
Author: Craig Duncan <git@duncanc.co.uk> Thu, 24 Nov 2016 21:56:53 +0000
Committer: Joe Watkins <krakjoe@php.net> Tue, 9 May 2017 10:17:19 +0100
Parents: e1c32646b40b6e3a41cbd7a78d03900cfdf1b72a
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=e946d074dd6df1f99b844c84e7179eb09f11c602
Log:
Ensure number_format() doesn't include sign for zero
Changed paths:
M UPGRADING
M ext/standard/math.c
A ext/standard/tests/math/number_format_negative_zero.phpt
Diff:
diff --git a/UPGRADING b/UPGRADING
index 78ce94c..0530958 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 169d0a0..794128b 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 0000000..743a535
--- /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--
+<?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"