From 0478e2ecbca1ac357279d1bd1e691bb8b45498ea Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 19 Jun 2023 23:46:52 +0200 Subject: [PATCH 1/2] number_format() Support rounding negative places --- ext/standard/math.c | 2 +- .../tests/math/number_format_negative_zero.phpt | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index ad2823ea49bf6..9ed51bf45fc57 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -1030,8 +1030,8 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de d = -d; } - dec = MAX(0, dec); d = _php_math_round(d, dec, PHP_ROUND_HALF_UP); + dec = MAX(0, dec); tmpbuf = strpprintf(0, "%.*F", dec, d); if (tmpbuf == NULL) { return NULL; diff --git a/ext/standard/tests/math/number_format_negative_zero.phpt b/ext/standard/tests/math/number_format_negative_zero.phpt index 743a5350e27cc..49f59cfb707a0 100644 --- a/ext/standard/tests/math/number_format_negative_zero.phpt +++ b/ext/standard/tests/math/number_format_negative_zero.phpt @@ -4,13 +4,28 @@ Prevent number_format from returning negative zero --EXPECT-- float(-1.15E-15) string(4) "0.00" +float(-0.01) string(5) "-0.01" +float(-1234567.89) +string(10) "-1,235,000" +float(-1234321.09) +string(10) "-1,234,000" From 907b554cbb0ec96fa0d6d0d7d651da0419e86822 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Fri, 23 Jun 2023 22:28:39 +0200 Subject: [PATCH 2/2] Added new test file and reverted modified test file --- .../tests/math/number_format_decimals.phpt | 151 ++++++++++++++++++ .../math/number_format_negative_zero.phpt | 17 +- 2 files changed, 152 insertions(+), 16 deletions(-) create mode 100644 ext/standard/tests/math/number_format_decimals.phpt diff --git a/ext/standard/tests/math/number_format_decimals.phpt b/ext/standard/tests/math/number_format_decimals.phpt new file mode 100644 index 0000000000000..fa1e96b76c257 --- /dev/null +++ b/ext/standard/tests/math/number_format_decimals.phpt @@ -0,0 +1,151 @@ +--TEST-- +Test number_format() - test function with different decimal places +--FILE-- + +--EXPECT-- +testing float(1.5151) +... with decimal places of 0: string(1) "2" +... with decimal places of 1: string(3) "1.5" +... with decimal places of 2: string(4) "1.52" +... with decimal places of 3: string(5) "1.515" +... with decimal places of 4: string(6) "1.5151" +... with decimal places of 5: string(7) "1.51510" +... with decimal places of -1: string(1) "0" +... with decimal places of -2: string(1) "0" +... with decimal places of -3: string(1) "0" +... with decimal places of -4: string(1) "0" +... with decimal places of -5: string(1) "0" +testing float(15.151) +... with decimal places of 0: string(2) "15" +... with decimal places of 1: string(4) "15.2" +... with decimal places of 2: string(5) "15.15" +... with decimal places of 3: string(6) "15.151" +... with decimal places of 4: string(7) "15.1510" +... with decimal places of 5: string(8) "15.15100" +... with decimal places of -1: string(2) "20" +... with decimal places of -2: string(1) "0" +... with decimal places of -3: string(1) "0" +... with decimal places of -4: string(1) "0" +... with decimal places of -5: string(1) "0" +testing float(151.51) +... with decimal places of 0: string(3) "152" +... with decimal places of 1: string(5) "151.5" +... with decimal places of 2: string(6) "151.51" +... with decimal places of 3: string(7) "151.510" +... with decimal places of 4: string(8) "151.5100" +... with decimal places of 5: string(9) "151.51000" +... with decimal places of -1: string(3) "150" +... with decimal places of -2: string(3) "200" +... with decimal places of -3: string(1) "0" +... with decimal places of -4: string(1) "0" +... with decimal places of -5: string(1) "0" +testing float(1515.1) +... with decimal places of 0: string(5) "1,515" +... with decimal places of 1: string(7) "1,515.1" +... with decimal places of 2: string(8) "1,515.10" +... with decimal places of 3: string(9) "1,515.100" +... with decimal places of 4: string(10) "1,515.1000" +... with decimal places of 5: string(11) "1,515.10000" +... with decimal places of -1: string(5) "1,520" +... with decimal places of -2: string(5) "1,500" +... with decimal places of -3: string(5) "2,000" +... with decimal places of -4: string(1) "0" +... with decimal places of -5: string(1) "0" +testing int(15151) +... with decimal places of 0: string(6) "15,151" +... with decimal places of 1: string(8) "15,151.0" +... with decimal places of 2: string(9) "15,151.00" +... with decimal places of 3: string(10) "15,151.000" +... with decimal places of 4: string(11) "15,151.0000" +... with decimal places of 5: string(12) "15,151.00000" +... with decimal places of -1: string(6) "15,150" +... with decimal places of -2: string(6) "15,200" +... with decimal places of -3: string(6) "15,000" +... with decimal places of -4: string(6) "20,000" +... with decimal places of -5: string(1) "0" +testing float(-1.5151) +... with decimal places of 0: string(2) "-2" +... with decimal places of 1: string(4) "-1.5" +... with decimal places of 2: string(5) "-1.52" +... with decimal places of 3: string(6) "-1.515" +... with decimal places of 4: string(7) "-1.5151" +... with decimal places of 5: string(8) "-1.51510" +... with decimal places of -1: string(1) "0" +... with decimal places of -2: string(1) "0" +... with decimal places of -3: string(1) "0" +... with decimal places of -4: string(1) "0" +... with decimal places of -5: string(1) "0" +testing float(-15.151) +... with decimal places of 0: string(3) "-15" +... with decimal places of 1: string(5) "-15.2" +... with decimal places of 2: string(6) "-15.15" +... with decimal places of 3: string(7) "-15.151" +... with decimal places of 4: string(8) "-15.1510" +... with decimal places of 5: string(9) "-15.15100" +... with decimal places of -1: string(3) "-20" +... with decimal places of -2: string(1) "0" +... with decimal places of -3: string(1) "0" +... with decimal places of -4: string(1) "0" +... with decimal places of -5: string(1) "0" +testing float(-151.51) +... with decimal places of 0: string(4) "-152" +... with decimal places of 1: string(6) "-151.5" +... with decimal places of 2: string(7) "-151.51" +... with decimal places of 3: string(8) "-151.510" +... with decimal places of 4: string(9) "-151.5100" +... with decimal places of 5: string(10) "-151.51000" +... with decimal places of -1: string(4) "-150" +... with decimal places of -2: string(4) "-200" +... with decimal places of -3: string(1) "0" +... with decimal places of -4: string(1) "0" +... with decimal places of -5: string(1) "0" +testing float(-1515.1) +... with decimal places of 0: string(6) "-1,515" +... with decimal places of 1: string(8) "-1,515.1" +... with decimal places of 2: string(9) "-1,515.10" +... with decimal places of 3: string(10) "-1,515.100" +... with decimal places of 4: string(11) "-1,515.1000" +... with decimal places of 5: string(12) "-1,515.10000" +... with decimal places of -1: string(6) "-1,520" +... with decimal places of -2: string(6) "-1,500" +... with decimal places of -3: string(6) "-2,000" +... with decimal places of -4: string(1) "0" +... with decimal places of -5: string(1) "0" +testing int(-15151) +... with decimal places of 0: string(7) "-15,151" +... with decimal places of 1: string(9) "-15,151.0" +... with decimal places of 2: string(10) "-15,151.00" +... with decimal places of 3: string(11) "-15,151.000" +... with decimal places of 4: string(12) "-15,151.0000" +... with decimal places of 5: string(13) "-15,151.00000" +... with decimal places of -1: string(7) "-15,150" +... with decimal places of -2: string(7) "-15,200" +... with decimal places of -3: string(7) "-15,000" +... with decimal places of -4: string(7) "-20,000" +... with decimal places of -5: string(1) "0" diff --git a/ext/standard/tests/math/number_format_negative_zero.phpt b/ext/standard/tests/math/number_format_negative_zero.phpt index 49f59cfb707a0..743a5350e27cc 100644 --- a/ext/standard/tests/math/number_format_negative_zero.phpt +++ b/ext/standard/tests/math/number_format_negative_zero.phpt @@ -4,28 +4,13 @@ Prevent number_format from returning negative zero --EXPECT-- float(-1.15E-15) string(4) "0.00" -float(-0.01) string(5) "-0.01" -float(-1234567.89) -string(10) "-1,235,000" -float(-1234321.09) -string(10) "-1,234,000"