Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 5262

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Format a number with grouped thousands</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.nl2br.html">nl2br</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.ord.html">ord</a></div>
 <div class="up"><a href="ref.strings.html">String Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><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)</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="type">string</span> <span class="methodname"><strong>number_format</strong></span>
    ( <span class="methodparam"><span class="type">float</span> <code class="parameter">$number</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$decimals</code><span class="initializer"> = 0</span></span>
  ] )</div>

  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>number_format</strong></span>
    ( <span class="methodparam"><span class="type">float</span> <code class="parameter">$number</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$decimals</code><span class="initializer"> = 0</span></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$dec_point</code><span class="initializer"> = &quot;.&quot;</span></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$thousands_sep</code><span class="initializer"> = &quot;,&quot;</span></span>
   )</div>

  <p class="para rdfs-comment">
   This function accepts either one, two, or four parameters (not three):
  </p>
  <p class="para">
   If only one parameter is given,
   <code class="parameter">number</code> will be formatted without decimals,
   but with a comma (&quot;,&quot;) between every group of thousands.
  </p>
  <p class="para">
   If two parameters are given, <code class="parameter">number</code> will
   be formatted with <code class="parameter">decimals</code> decimals with a
   dot (&quot;.&quot;) in front, and a comma (&quot;,&quot;) between every group of
   thousands.
  </p>
  <p class="para">
   If all four parameters are given, <code class="parameter">number</code>
   will be formatted with <code class="parameter">decimals</code> decimals,
   <code class="parameter">dec_point</code> instead of a dot (&quot;.&quot;) before
   the decimals and <code class="parameter">thousands_sep</code> instead of
   a comma (&quot;,&quot;) between every group of thousands.
  </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">number</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 points.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">dec_point</code></dt>

     <dd>

      <p class="para">
       Sets the separator for the decimal point.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">thousands_sep</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">number</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>7.2.0</td>
       <td>
        <span class="function"><strong>number_format()</strong></span> was changed to not being able to return 
        <em>-0</em>, previously <em>-0</em> could be returned 
        for cases like where <code class="parameter">number</code> would be <em>-0.01</em>.
       </td>
      </tr>

      <tr>
       <td>5.4.0</td>
       <td>
        This function now supports multiple bytes in
        <code class="parameter">dec_point</code> and
        <code class="parameter">thousands_sep</code>. Only the first byte of each
        separator was used in older versions.
       </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-6057">
    <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="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1234.56</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;english&nbsp;notation&nbsp;(default)<br /></span><span style="color: #0000BB">$english_format_number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;1,235<br /><br />//&nbsp;French&nbsp;notation<br /></span><span style="color: #0000BB">$nombre_format_francais&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">','</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&nbsp;'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;1&nbsp;234,56<br /><br /></span><span style="color: #0000BB">$number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1234.5678</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;english&nbsp;notation&nbsp;without&nbsp;thousands&nbsp;separator<br /></span><span style="color: #0000BB">$english_format_number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'.'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;1234.57<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

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


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.nl2br.html">nl2br</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.ord.html">ord</a></div>
 <div class="up"><a href="ref.strings.html">String Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>