Skip to content

Commit

Permalink
Document number_format() handling of negative $decimals (closes #4200) (
Browse files Browse the repository at this point in the history
#4201)

Co-authored-by: Gina Peter Banyard <girgias@php.net>
  • Loading branch information
jimwins and Girgias authored Dec 1, 2024
1 parent 9ab074d commit ae14882
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions reference/strings/functions/number-format.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
Sets the number of decimal digits.
If <literal>0</literal>, the <parameter>decimal_separator</parameter> is
omitted from the return value.
As of PHP 8.3.0, when the value is negative, <parameter>num</parameter>
is rounded to <parameter>decimals</parameter> significant digits before
the decimal point.
Prior to PHP 8.3.0, negative values were ignored and handled the
same as <literal>0</literal>.
</para>
</listitem>
</varlistentry>
Expand Down Expand Up @@ -81,6 +86,12 @@
</row>
</thead>
<tbody>
<row>
<entry>8.3.0</entry>
<entry>
Added handling of negative values for <parameter>decimals</parameter>.
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
Expand Down Expand Up @@ -137,6 +148,32 @@ $english_format_number = number_format($number, 2, '.', '');
</programlisting>
</example>
</para>
<example>
<title>A negative value for <parameter>decimals</parameter></title>
<simpara>
As of PHP 8.3.0, a negative value for <parameter>decimals</parameter>
is used to round the number of significant digits before the decimal
point.
</simpara>
<programlisting role="php">
<![CDATA[
<?php
$number = "1234.5678";
var_dump(number_format($number, -1));
var_dump(number_format($number, -2));
var_dump(number_format($number, -3));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(5) "1,230"
string(5) "1,200"
string(5) "1,000"
]]>
</screen>
</example>
</refsect1>

<refsect1 role="seealso">
Expand Down

0 comments on commit ae14882

Please sign in to comment.