PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<randsin>
view the version of this page
Last updated: Thu, 21 Aug 2003

round

(PHP 3, PHP 4 )

round -- Rounds a float

Description

float round ( float val [, int precision])

Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

Example 1. round() examples

<?php
echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4
echo round(3.6, 0);      // 4
echo round(1.95583, 2);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2);    // 5.04
echo round(5.055, 2);    // 5.06
?>

Caution

When rounding on exact halves round() rounds down on evens and up on odds. If you want to always force it in one direction on a .5 (or .05 in your case) add or substract a tiny fuzz factor. The reason behind rounding half the values down and the other half up is to avoid the classical banking problem where if you always rounded down you would be stealing money from your customers, or if you always rounded up you would end up over time losing money. By averaging it out through evens and odds you statistically break even.

Note: PHP doesn't handle strings like "12,300.2" correctly by default. See converting from strings.

Note: The precision parameter is only available in PHP 4.

See also ceil(), floor() and number_format().



add a note add a note User Contributed Notes
round
26-Aug-2003 06:12
Many have thus far mentioned problems encountered when trying to add a small fuzz factor to a number such as 1.499999999.  This is the way I get around that problem using , allbeit probably less efficient than assuming a small possiblitiy for error:

$numberToRound = 1.5;

//Convert to string.
$numberToRound = "$numberToRound";

//iff number ends in a "5", add fuzz
if (eregi("$5", $pages)) $pages += .000001;   

$round = round($pages, 0);
ianring (at) golden.net
08-Aug-2003 02:30
The round() function may indeed work properly with half-values (eg. 1.5), but this little method will give you peace of mind. Add some "fuzz" to your function with a miniscule delta value.

$delta = 0.00001;
$x = round($x+$delta);

This is fine, unless $x has a value of 1.49999 ... if you worried about that, use this method instead:

if(($x-floor($x))==0.5){
   $x+=$delta;
}
$x = round($x);

you can change your "optimistic" delta into a "pessimistic" delta by subtracting instead of adding.

Cheers,
Ian Ring
ralf dot schreijer at smc dot uni-muenster dot de
18-Jun-2003 07:39
The function below regards a higher number of digits for rounding as the number of digits you want to round! At least it rounds a Value to the number of digits you want to:

function MyRound($iValue, $iDigits, $iPrecision){
   $iDigits = intval($iDigits);
   $iPrecision = intval($iPrecision);
   if($iDigits > $iPrecision){ $iPrecision = $iDigits; }
   for($i = $iPrecision; $i >= $iDigits; $i--){
      $iValue = round($iValue, $i);
   } // for($i = $iPrecision; $i >= $iDigits; $i--) -- END
   return $iValue;
}
Oromian
13-May-2003 07:08
<?
// Rounding to the nearest fifth
// or any other increment you wish...

$percent "48";
 
$num round($percent/5)*5;
    echo 
$num;
   
// returns 50

$percentt "47";
 
$numm round($percentt/5)*5;
    echo 
$numm;
   
// returns 45
?>
programmer at cncgn dot com
16-Mar-2003 02:29
The Following will tell you how much sapce you have in MB and how much space is left in MB, if you wont to know in Bytes then change

$df = round(( disk_total_space("/") / 1000000 ));
$dg = round(( disk_free_space("/") / 1000000 ));

to

$df = disk_total_space("/");
$dg = disk_free_space("/");

<?php
$df 
round(( disk_total_space("/") / 1000000 ));
$dg round(( disk_free_space("/") / 1000000 ));
$dh "<br> <b>Total Disk Space : ".$df."MB<br> Free Disk Speace : ".$dg."MB</b><br>";
Echo 
$dh;
?>
eb at st-us dot com
18-Jan-2003 03:16
/*next line will output 1.4999 as 1.49, no round. Use it if you want to collect fractional money less than 0.01 after for example % calculations.*/
$a=1.4999;
$a = sprintf("%01.2lf", floor($a*100)/100); //$a=1.49
tichoux at charlevoix dot net
24-Oct-2002 04:06
for a poll, if you want to have 100% and not 99 or 99.99 % you can do that :

round( number_format( (($individual_result*100)/$total_result), 2), 1)
kt at netspirit dot ch
20-Aug-2002 11:10
because of some site-effects between round() and modulo, therfore the result is not exactly at  every time ...
another way ( and without site-effects) of getting 0.05 increments for currencies, f.e. swiss francs:

$res = (round(20*$chf))/20;
echo $res;

with kind regards
php at silisoftware dot com
15-Aug-2002 04:15
Here's a function to round to an arbitary number of significant digits. Don't confuse it with rounding to a negative precision - that counts back from the decimal point, this function counts forward from the Most Significant Digit.

ex:
round(1241757, -3); // 1242000
RoundSigDigs(1241757, 3); // 1240000

Works on negative numbers too. $sigdigs should be >= 0

function RoundSigDigs($number, $sigdigs) {
    $multiplier = 1;
    while ($number < 0.1) {
        $number *= 10;
        $multiplier /= 10;
    }
    while ($number >= 1) {
        $number /= 10;
        $multiplier *= 10;
    }
    return round($number, $sigdigs) * $multiplier;
}
dalef at bendnet dot com
26-Mar-2002 03:27
for php 3.x, if you need to use the precision arg:

function round3x($val,$precision){
$exp = pow(10,$precision);
$val = $val *  $exp;
$val = round($val);
$val = $val /  $exp;
return $val;
}
js5 at sanger dot ac dot uk
18-Jan-2002 03:17
This isn't a bug. Rounding of "halves" is usally done to the even number. This means that on average half of the "halves" are rounded up and half of the "halves" are rounded down. So that
any "average" calculated should not be affected by the rounding.
felix at innovative-web dot NOSP dot AM dot de
31-Dec-2001 10:07
Hi!

Yet another update to the bcround function. Vittorio Moccia pointed out that it didn't work with "infinite" precision. However, the function below allows you to gain the precision you need. $precision specifies the number of decimals behind $decimals that shall be used for rounding (10 or 20 is a good value). Hope this was the last bug in the function. If you find any more, don't hesitate to contact me.

function bcround ($number, $decimals, $precision)
{
 $precision_add = str_repeat("5",$precision);

 if (bccomp($number,0) > 0)
 {
  return (bcadd (bcadd ($number, "0.".str_repeat("0",$decimals).$precision_add, $decimals+$precision), 0, $decimals));
 }
 else
 {
  return (bcadd (bcsub ($number, "0.".str_repeat("0",$decimals).$precision_add, $decimals+$precision), 0, $decimals));
 }
}

- Felix Schwarz, Innovative
twan at ecreation dot nl
16-May-2000 08:51
If you'd only want to round for displaying variables (not for calculating on the rounded result) then you should use printf with the float:
printf ("%6.2f",3.39532);

This returns: 3.40 .

<randsin>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: http://php.mirrors.ilisys.com.au/
Last updated: Sat 01 Nov 2003 04:13:36 EST EST