Skip to content

Commit e946d07

Browse files
duncan3dckrakjoe
authored andcommitted
Ensure number_format() doesn't include sign for zero
1 parent e1c3264 commit e946d07

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ See also: https://wiki.php.net/rfc/deprecations_php_7_2
187187
. count() now raises a warning when an invalid parameter is passed.
188188
Only arrays and objects implementing the Countable interface should be passed.
189189
. pack() and unpack() now support float and double in both little and big endian.
190+
. number_format() ensures zero values never contain a negative sign.
190191

191192
- XML:
192193
. utf8_encode() and utf8_decode() have been moved to the Standard extension

ext/standard/math.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,11 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, char *dec_poin
11431143
return tmpbuf;
11441144
}
11451145

1146+
/* Check if the number is no longer negative after rounding */
1147+
if (is_negative && d == 0) {
1148+
is_negative = 0;
1149+
}
1150+
11461151
/* find decimal point, if expected */
11471152
if (dec) {
11481153
dp = strpbrk(ZSTR_VAL(tmpbuf), ".,");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Prevent number_format from returning negative zero
3+
--FILE--
4+
<?php
5+
6+
$number = -1.15E-15;
7+
8+
var_dump($number);
9+
var_dump(number_format($number, 2));
10+
var_dump(number_format(-0.01, 2));
11+
12+
?>
13+
--EXPECT--
14+
float(-1.15E-15)
15+
string(4) "0.00"
16+
string(5) "-0.01"

0 commit comments

Comments
 (0)