Skip to content

Commit ae14882

Browse files
jimwinsGirgias
andauthored
Document number_format() handling of negative $decimals (closes #4200) (#4201)
Co-authored-by: Gina Peter Banyard <[email protected]>
1 parent 9ab074d commit ae14882

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

reference/strings/functions/number-format.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
Sets the number of decimal digits.
4040
If <literal>0</literal>, the <parameter>decimal_separator</parameter> is
4141
omitted from the return value.
42+
As of PHP 8.3.0, when the value is negative, <parameter>num</parameter>
43+
is rounded to <parameter>decimals</parameter> significant digits before
44+
the decimal point.
45+
Prior to PHP 8.3.0, negative values were ignored and handled the
46+
same as <literal>0</literal>.
4247
</para>
4348
</listitem>
4449
</varlistentry>
@@ -81,6 +86,12 @@
8186
</row>
8287
</thead>
8388
<tbody>
89+
<row>
90+
<entry>8.3.0</entry>
91+
<entry>
92+
Added handling of negative values for <parameter>decimals</parameter>.
93+
</entry>
94+
</row>
8495
<row>
8596
<entry>8.0.0</entry>
8697
<entry>
@@ -137,6 +148,32 @@ $english_format_number = number_format($number, 2, '.', '');
137148
</programlisting>
138149
</example>
139150
</para>
151+
<example>
152+
<title>A negative value for <parameter>decimals</parameter></title>
153+
<simpara>
154+
As of PHP 8.3.0, a negative value for <parameter>decimals</parameter>
155+
is used to round the number of significant digits before the decimal
156+
point.
157+
</simpara>
158+
<programlisting role="php">
159+
<![CDATA[
160+
<?php
161+
$number = "1234.5678";
162+
var_dump(number_format($number, -1));
163+
var_dump(number_format($number, -2));
164+
var_dump(number_format($number, -3));
165+
?>
166+
]]>
167+
</programlisting>
168+
&example.outputs;
169+
<screen>
170+
<![CDATA[
171+
string(5) "1,230"
172+
string(5) "1,200"
173+
string(5) "1,000"
174+
]]>
175+
</screen>
176+
</example>
140177
</refsect1>
141178

142179
<refsect1 role="seealso">

0 commit comments

Comments
 (0)