Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e85fb09

Browse files
marc-mabebukka
authored andcommitted
number_format() Support rounding negative places
Closes GH-11487
1 parent 7c5a570 commit e85fb09

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ PHP NEWS
2323
. Fixed GH-11573 (RecursiveDirectoryIterator::hasChildren is slow).
2424
(nielsdos)
2525

26+
- Standard:
27+
. Added support for rounding negative places in number_format().
28+
(Marc Bennewitz)
29+
2630
- Streams:
2731
. Implemented GH-11242 (_php_stream_copy_to_mem: Allow specifying a maximum
2832
length without allocating a buffer of that size). (Jakub Zelenka)

‎UPGRADING‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ PHP 8.3 UPGRADE NOTES
219219
Previously arrays and objects where ignored whilst every other value was cast to int.
220220
Moreover, objects that define a numeric cast (e.g. GMP) are now casted instead of ignored.
221221
RFC: https://wiki.php.net/rfc/saner-array-sum-product
222+
. number_format() $decimal parameter handles rounding to negative places. It
223+
means that when $decimals is negative, $num is rounded to $decimals
224+
significant digits before the decimal point. Previously negative $decimals
225+
got silently ignored and the number got rounded to zero decimal places.
222226

223227
========================================
224228
6. New Functions

‎ext/standard/math.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,8 +1030,8 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de
10301030
d = -d;
10311031
}
10321032

1033-
dec = MAX(0, dec);
10341033
d = _php_math_round(d, dec, PHP_ROUND_HALF_UP);
1034+
dec = MAX(0, dec);
10351035
tmpbuf = strpprintf(0, "%.*F", dec, d);
10361036
if (tmpbuf == NULL) {
10371037
return NULL;
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
--TEST--
2+
Test number_format() - test function with different decimal places
3+
--FILE--
4+
<?php
5+
$values = array(
6+
1.5151,
7+
15.151,
8+
151.51,
9+
1515.1,
10+
15151,
11+
-1.5151,
12+
-15.151,
13+
-151.51,
14+
-1515.1,
15+
-15151
16+
);
17+
18+
$decimals = array(0, 1, 2, 3, 4, 5, -1, -2, -3, -4, -5);
19+
20+
foreach ($values as $value) {
21+
echo 'testing ';
22+
var_dump($value);
23+
24+
foreach ($decimals as $decimal) {
25+
echo '... with decimal places of ' . $decimal . ': ';
26+
var_dump(number_format($value, $decimal));
27+
}
28+
}
29+
30+
?>
31+
--EXPECT--
32+
testing float(1.5151)
33+
... with decimal places of 0: string(1) "2"
34+
... with decimal places of 1: string(3) "1.5"
35+
... with decimal places of 2: string(4) "1.52"
36+
... with decimal places of 3: string(5) "1.515"
37+
... with decimal places of 4: string(6) "1.5151"
38+
... with decimal places of 5: string(7) "1.51510"
39+
... with decimal places of -1: string(1) "0"
40+
... with decimal places of -2: string(1) "0"
41+
... with decimal places of -3: string(1) "0"
42+
... with decimal places of -4: string(1) "0"
43+
... with decimal places of -5: string(1) "0"
44+
testing float(15.151)
45+
... with decimal places of 0: string(2) "15"
46+
... with decimal places of 1: string(4) "15.2"
47+
... with decimal places of 2: string(5) "15.15"
48+
... with decimal places of 3: string(6) "15.151"
49+
... with decimal places of 4: string(7) "15.1510"
50+
... with decimal places of 5: string(8) "15.15100"
51+
... with decimal places of -1: string(2) "20"
52+
... with decimal places of -2: string(1) "0"
53+
... with decimal places of -3: string(1) "0"
54+
... with decimal places of -4: string(1) "0"
55+
... with decimal places of -5: string(1) "0"
56+
testing float(151.51)
57+
... with decimal places of 0: string(3) "152"
58+
... with decimal places of 1: string(5) "151.5"
59+
... with decimal places of 2: string(6) "151.51"
60+
... with decimal places of 3: string(7) "151.510"
61+
... with decimal places of 4: string(8) "151.5100"
62+
... with decimal places of 5: string(9) "151.51000"
63+
... with decimal places of -1: string(3) "150"
64+
... with decimal places of -2: string(3) "200"
65+
... with decimal places of -3: string(1) "0"
66+
... with decimal places of -4: string(1) "0"
67+
... with decimal places of -5: string(1) "0"
68+
testing float(1515.1)
69+
... with decimal places of 0: string(5) "1,515"
70+
... with decimal places of 1: string(7) "1,515.1"
71+
... with decimal places of 2: string(8) "1,515.10"
72+
... with decimal places of 3: string(9) "1,515.100"
73+
... with decimal places of 4: string(10) "1,515.1000"
74+
... with decimal places of 5: string(11) "1,515.10000"
75+
... with decimal places of -1: string(5) "1,520"
76+
... with decimal places of -2: string(5) "1,500"
77+
... with decimal places of -3: string(5) "2,000"
78+
... with decimal places of -4: string(1) "0"
79+
... with decimal places of -5: string(1) "0"
80+
testing int(15151)
81+
... with decimal places of 0: string(6) "15,151"
82+
... with decimal places of 1: string(8) "15,151.0"
83+
... with decimal places of 2: string(9) "15,151.00"
84+
... with decimal places of 3: string(10) "15,151.000"
85+
... with decimal places of 4: string(11) "15,151.0000"
86+
... with decimal places of 5: string(12) "15,151.00000"
87+
... with decimal places of -1: string(6) "15,150"
88+
... with decimal places of -2: string(6) "15,200"
89+
... with decimal places of -3: string(6) "15,000"
90+
... with decimal places of -4: string(6) "20,000"
91+
... with decimal places of -5: string(1) "0"
92+
testing float(-1.5151)
93+
... with decimal places of 0: string(2) "-2"
94+
... with decimal places of 1: string(4) "-1.5"
95+
... with decimal places of 2: string(5) "-1.52"
96+
... with decimal places of 3: string(6) "-1.515"
97+
... with decimal places of 4: string(7) "-1.5151"
98+
... with decimal places of 5: string(8) "-1.51510"
99+
... with decimal places of -1: string(1) "0"
100+
... with decimal places of -2: string(1) "0"
101+
... with decimal places of -3: string(1) "0"
102+
... with decimal places of -4: string(1) "0"
103+
... with decimal places of -5: string(1) "0"
104+
testing float(-15.151)
105+
... with decimal places of 0: string(3) "-15"
106+
... with decimal places of 1: string(5) "-15.2"
107+
... with decimal places of 2: string(6) "-15.15"
108+
... with decimal places of 3: string(7) "-15.151"
109+
... with decimal places of 4: string(8) "-15.1510"
110+
... with decimal places of 5: string(9) "-15.15100"
111+
... with decimal places of -1: string(3) "-20"
112+
... with decimal places of -2: string(1) "0"
113+
... with decimal places of -3: string(1) "0"
114+
... with decimal places of -4: string(1) "0"
115+
... with decimal places of -5: string(1) "0"
116+
testing float(-151.51)
117+
... with decimal places of 0: string(4) "-152"
118+
... with decimal places of 1: string(6) "-151.5"
119+
... with decimal places of 2: string(7) "-151.51"
120+
... with decimal places of 3: string(8) "-151.510"
121+
... with decimal places of 4: string(9) "-151.5100"
122+
... with decimal places of 5: string(10) "-151.51000"
123+
... with decimal places of -1: string(4) "-150"
124+
... with decimal places of -2: string(4) "-200"
125+
... with decimal places of -3: string(1) "0"
126+
... with decimal places of -4: string(1) "0"
127+
... with decimal places of -5: string(1) "0"
128+
testing float(-1515.1)
129+
... with decimal places of 0: string(6) "-1,515"
130+
... with decimal places of 1: string(8) "-1,515.1"
131+
... with decimal places of 2: string(9) "-1,515.10"
132+
... with decimal places of 3: string(10) "-1,515.100"
133+
... with decimal places of 4: string(11) "-1,515.1000"
134+
... with decimal places of 5: string(12) "-1,515.10000"
135+
... with decimal places of -1: string(6) "-1,520"
136+
... with decimal places of -2: string(6) "-1,500"
137+
... with decimal places of -3: string(6) "-2,000"
138+
... with decimal places of -4: string(1) "0"
139+
... with decimal places of -5: string(1) "0"
140+
testing int(-15151)
141+
... with decimal places of 0: string(7) "-15,151"
142+
... with decimal places of 1: string(9) "-15,151.0"
143+
... with decimal places of 2: string(10) "-15,151.00"
144+
... with decimal places of 3: string(11) "-15,151.000"
145+
... with decimal places of 4: string(12) "-15,151.0000"
146+
... with decimal places of 5: string(13) "-15,151.00000"
147+
... with decimal places of -1: string(7) "-15,150"
148+
... with decimal places of -2: string(7) "-15,200"
149+
... with decimal places of -3: string(7) "-15,000"
150+
... with decimal places of -4: string(7) "-20,000"
151+
... with decimal places of -5: string(1) "0"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /