<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.number-format.php',
    1 => 'number_format',
    2 => 'Format a number with grouped thousands',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String Functions',
  ),
  'prev' => 
  array (
    0 => 'function.nl2br.php',
    1 => 'nl2br',
  ),
  'next' => 
  array (
    0 => 'function.ord.php',
    1 => 'ord',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/strings/functions/number-format.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.number-format" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">number_format</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">number_format</span> &mdash; <span class="dc-title">Format a number with grouped thousands</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.number-format-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>number_format</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.float.php" class="type float">float</a></span> <code class="parameter">$num</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$decimals</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$decimal_separator</code><span class="initializer"> = &quot;.&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$thousands_separator</code><span class="initializer"> = &quot;,&quot;</span></span><br>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   Formats a number with grouped thousands and optionally decimal digits using the rounding half up rule.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.number-format-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">num</code></dt>
     <dd>
      <p class="para">
       The number being formatted.
      </p>
     </dd>
    
    
     <dt><code class="parameter">decimals</code></dt>
     <dd>
      <p class="para">
       Sets the number of decimal digits.
       If <code class="literal">0</code>, the <code class="parameter">decimal_separator</code> is
       omitted from the return value.
       As of PHP 8.3.0, when the value is negative, <code class="parameter">num</code>
       is rounded to <code class="parameter">decimals</code> significant digits before
       the decimal point.
       Prior to PHP 8.3.0, negative values were ignored and handled the
       same as <code class="literal">0</code>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">decimal_separator</code></dt>
     <dd>
      <p class="para">
       Sets the separator for the decimal point.
      </p>
     </dd>
    
    
     <dt><code class="parameter">thousands_separator</code></dt>
     <dd>
      <p class="para">
       Sets the thousands separator.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.number-format-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   A formatted version of <code class="parameter">num</code>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.number-format-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.3.0</td>
       <td>
        Added handling of negative values for <code class="parameter">decimals</code>.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        Prior to this version, <span class="function"><strong>number_format()</strong></span> accepted
        one, two, or four parameters (but not three).
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <span class="function"><strong>number_format()</strong></span> was changed to not being able to return 
        <code class="literal">-0</code>, previously <code class="literal">-0</code> could be returned 
        for cases like where <code class="parameter">num</code> would be <code class="literal">-0.01</code>.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.number-format-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>number_format()</strong></span> Example</strong></p>
    <div class="example-contents"><p>
     For instance, French notation usually use two decimals,
     comma (&#039;,&#039;) as decimal separator, and space (&#039; &#039;) as
     thousand separator. The following example demonstrates various ways to format a number:
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$number </span><span style="color: #007700">= </span><span style="color: #0000BB">1234.56</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// english notation (default)<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// 1,235<br /><br />// French notation<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">','</span><span style="color: #007700">, </span><span style="color: #DD0000">' '</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// 1 234,56<br /><br /></span><span style="color: #0000BB">$number </span><span style="color: #007700">= </span><span style="color: #0000BB">1234.5678</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// english notation without thousands separator<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'.'</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// 1234.57<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <div class="example" id="example-2">
   <p><strong>Example #2 A negative value for <code class="parameter">decimals</code></strong></p>
   <div class="example-contents"><p>
    As of PHP 8.3.0, a negative value for <code class="parameter">decimals</code>
    is used to round the number of significant digits before the decimal
    point.
   </p></div>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$number </span><span style="color: #007700">= </span><span style="color: #DD0000">"1234.5678"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">, -</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">, -</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">string(5) &quot;1,230&quot;
string(5) &quot;1,200&quot;
string(5) &quot;1,000&quot;</pre>
</div>
   </div>
  </div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.number-format-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.money-format.php" class="function" rel="rdfs-seeAlso">money_format()</a> - Formats a number as a currency string</span></li>
    <li><span class="function"><a href="function.sprintf.php" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li>
    <li><span class="function"><a href="function.printf.php" class="function" rel="rdfs-seeAlso">printf()</a> - Output a formatted string</span></li>
    <li><span class="function"><a href="function.sscanf.php" class="function" rel="rdfs-seeAlso">sscanf()</a> - Parses input from a string according to a format</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>