<!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>Rounds a float</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.rand.html">rand</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.sin.html">sin</a></div> <div class="up"><a href="ref.math.html">Math Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.round" class="refentry"> <div class="refnamediv"> <h1 class="refname">round</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">round</span> — <span class="dc-title">Rounds a float</span></p> </div> <div class="refsect1 description" id="refsect1-function.round-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">float</span> <span class="methodname"><strong>round</strong></span> ( <span class="methodparam"><span class="type">float</span> <code class="parameter">$val</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$precision</code><span class="initializer"> = 0</span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = PHP_ROUND_HALF_UP</span></span> ]] )</div> <p class="para rdfs-comment"> Returns the rounded value of <em><code class="parameter">val</code></em> to specified <em><code class="parameter">precision</code></em> (number of digits after the decimal point). <em><code class="parameter">precision</code></em> can also be negative or zero (default). </p> <p class="para"> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> PHP doesn't handle strings like <em>"12,300.2"</em> correctly by default. See <a href="language.types.string.html#language.types.string.conversion" class="link">converting from strings</a>. </span> </p></blockquote> </p> </div> <div class="refsect1 parameters" id="refsect1-function.round-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">val</code></em></span> <dd> <p class="para"> The value to round </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">precision</code></em></span> <dd> <p class="para"> The optional number of decimal digits to round to. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">mode</code></em></span> <dd> <p class="para"> Use one of the following constants to specify the mode in which rounding occurs. <table class="doctable informaltable"> <thead> <tr> <th>Constant</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td><strong><code>PHP_ROUND_HALF_UP</code></strong></td> <td> Round <em><code class="parameter">val</code></em> up to <em><code class="parameter">precision</code></em> decimal places away from zero, when it is half way there. Making 1.5 into 2 and -1.5 into -2. </td> </tr> <tr> <td><strong><code>PHP_ROUND_HALF_DOWN</code></strong></td> <td> Round <em><code class="parameter">val</code></em> down to <em><code class="parameter">precision</code></em> decimal places towards zero, when it is half way there. Making 1.5 into 1 and -1.5 into -1. </td> </tr> <tr> <td><strong><code>PHP_ROUND_HALF_EVEN</code></strong></td> <td> Round <em><code class="parameter">val</code></em> to <em><code class="parameter">precision</code></em> decimal places towards the next even value. </td> </tr> <tr> <td><strong><code>PHP_ROUND_HALF_ODD</code></strong></td> <td> Round <em><code class="parameter">val</code></em> to <em><code class="parameter">precision</code></em> decimal places towards the next odd value. </td> </tr> </tbody> </table> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.round-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The rounded value </p> </div> <div class="refsect1 examples" id="refsect1-function.round-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3474"> <p><strong>Example #1 <span class="function"><strong>round()</strong></span> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">3.4</span><span style="color: #007700">); </span><span style="color: #FF8000">// 3<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">3.5</span><span style="color: #007700">); </span><span style="color: #FF8000">// 4<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">3.6</span><span style="color: #007700">); </span><span style="color: #FF8000">// 4<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">3.6</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">); </span><span style="color: #FF8000">// 4<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">1.95583</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1.96<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">1241757</span><span style="color: #007700">, -</span><span style="color: #0000BB">3</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1242000<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">5.045</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// 5.05<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">5.055</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// 5.06<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-3475"> <p><strong>Example #2 <em><code class="parameter">mode</code></em> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">9.5</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_UP</span><span style="color: #007700">); </span><span style="color: #FF8000">// 10<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">9.5</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_DOWN</span><span style="color: #007700">); </span><span style="color: #FF8000">// 9<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">9.5</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_EVEN</span><span style="color: #007700">); </span><span style="color: #FF8000">// 10<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">9.5</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_ODD</span><span style="color: #007700">); </span><span style="color: #FF8000">// 9<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">8.5</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_UP</span><span style="color: #007700">); </span><span style="color: #FF8000">// 9<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">8.5</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_DOWN</span><span style="color: #007700">); </span><span style="color: #FF8000">// 8<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">8.5</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_EVEN</span><span style="color: #007700">); </span><span style="color: #FF8000">// 8<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">8.5</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_ODD</span><span style="color: #007700">); </span><span style="color: #FF8000">// 9<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-3476"> <p><strong>Example #3 <em><code class="parameter">mode with precision</code></em> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* Using PHP_ROUND_HALF_UP with 1 decimal digit precision */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">( </span><span style="color: #0000BB">1.55</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_UP</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1.6<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">( </span><span style="color: #0000BB">1.54</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_UP</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1.5<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(-</span><span style="color: #0000BB">1.55</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_UP</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1.6<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(-</span><span style="color: #0000BB">1.54</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_UP</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1.5<br /><br />/* Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">( </span><span style="color: #0000BB">1.55</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_DOWN</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1.5<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">( </span><span style="color: #0000BB">1.54</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_DOWN</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1.5<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(-</span><span style="color: #0000BB">1.55</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_DOWN</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1.5<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(-</span><span style="color: #0000BB">1.54</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_DOWN</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1.5<br /><br />/* Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">( </span><span style="color: #0000BB">1.55</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_EVEN</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1.6<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">( </span><span style="color: #0000BB">1.54</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_EVEN</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1.5<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(-</span><span style="color: #0000BB">1.55</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_EVEN</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1.6<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(-</span><span style="color: #0000BB">1.54</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_EVEN</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1.5<br /><br />/* Using PHP_ROUND_HALF_ODD with 1 decimal digit precision */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">( </span><span style="color: #0000BB">1.55</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_ODD</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1.5<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">( </span><span style="color: #0000BB">1.54</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_ODD</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1.5<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(-</span><span style="color: #0000BB">1.55</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_ODD</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1.5<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">round</span><span style="color: #007700">(-</span><span style="color: #0000BB">1.54</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_ROUND_HALF_ODD</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1.5<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 changelog" id="refsect1-function.round-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>5.3.0</td> <td> The <em><code class="parameter">mode</code></em> parameter was introduced. </td> </tr> <tr> <td>5.2.7</td> <td> The inner workings of <span class="function"><strong>round()</strong></span> was changed to conform to the C99 standard. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 seealso" id="refsect1-function.round-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.ceil.html" class="function" rel="rdfs-seeAlso">ceil()</a> - Round fractions up</span></li> <li class="member"> <span class="function"><a href="function.floor.html" class="function" rel="rdfs-seeAlso">floor()</a> - Round fractions down</span></li> <li class="member"> <span class="function"><a href="function.number-format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - Format a number with grouped thousands</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.rand.html">rand</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.sin.html">sin</a></div> <div class="up"><a href="ref.math.html">Math Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>