Here is function that rounds to a specified increment, but always up. I had to use it for price adjustment that always went up to $5 increments.
<?php
function roundUpTo($number, $increments) {
$increments = 1 / $increments;
return (ceil($number * $increments) / $increments);
}
?>
round
(PHP 4, PHP 5)
round — Rounds a float
Description
float round
( float
$val
[, int $precision = 0
[, int $mode = PHP_ROUND_HALF_UP
]] )
Returns the rounded value of val to
specified precision
(number of digits after the decimal point).
precision can also be negative or zero (default).
Note: PHP doesn't handle strings like "12,300.2" correctly by default. See converting from strings.
Parameters
-
val -
The value to round
-
precision -
The optional number of decimal digits to round to.
-
mode -
Use one of the following constants to specify the mode in which rounding occurs.
Constant Description PHP_ROUND_HALF_UPRound valup toprecisiondecimal places away from zero, when it is half way there. Making 1.5 into 2 and -1.5 into -2.PHP_ROUND_HALF_DOWNRound valdown toprecisiondecimal places towards zero, when it is half way there. Making 1.5 into 1 and -1.5 into -1.PHP_ROUND_HALF_EVENRound valtoprecisiondecimal places towards the next even value.PHP_ROUND_HALF_ODDRound valtoprecisiondecimal places towards the next odd value.
Return Values
The rounded value
Examples
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.05
echo round(5.055, 2); // 5.06
?>
Example #2 mode examples
<?php
echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10
echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9
echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10
echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9
echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9
echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9
?>
Example #3 mode with precision examples
<?php
/* Using PHP_ROUND_HALF_UP with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_UP); // 1.6
echo round( 1.54, 1, PHP_ROUND_HALF_UP); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_UP); // -1.6
echo round(-1.54, 1, PHP_ROUND_HALF_UP); // -1.5
/* Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_DOWN); // 1.5
echo round( 1.54, 1, PHP_ROUND_HALF_DOWN); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_DOWN); // -1.5
echo round(-1.54, 1, PHP_ROUND_HALF_DOWN); // -1.5
/* Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_EVEN); // 1.6
echo round( 1.54, 1, PHP_ROUND_HALF_EVEN); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_EVEN); // -1.6
echo round(-1.54, 1, PHP_ROUND_HALF_EVEN); // -1.5
/* Using PHP_ROUND_HALF_ODD with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_ODD); // 1.5
echo round( 1.54, 1, PHP_ROUND_HALF_ODD); // 1.5
echo round(-1.55, 1, PHP_ROUND_HALF_ODD); // -1.5
echo round(-1.54, 1, PHP_ROUND_HALF_ODD); // -1.5
?>
Changelog
| Version | Description |
|---|---|
| 5.3.0 |
The mode parameter was introduced.
|
| 5.2.7 | The inner workings of round() was changed to conform to the C99 standard. |
See Also
- ceil() - Round fractions up
- floor() - Round fractions down
- number_format() - Format a number with grouped thousands
Anonymous ¶
2 years ago
terry at scribendi dot com ¶
9 years ago
To round any number to a given number of significant digits, use log10 to find out its magnitude:
<?php round($n, ceil(0 - log10($n)) + $sigdigits); ?>
Or when you have to display a per-unit price which may work out to be less than a few cents/pence/yen you can use:
<?php
// $exp = currency decimal places - 0 for Yen/Won, 2 for most others
$dp = ceil(0 - log10($n)) + $sigdigits;
$display = number_format($amount, ($exp>$dp)?$exp:$dp);
?>
This always displays at least the number of decimal places required by the currency, but more if displaying the unit price with precision requires it - eg: 'English proofreading from $0.0068 per word', 'English beer from $6.80 per pint'.
dastra ¶
11 months ago
round() will sometimes return E notation when rounding a float when the amount is small enough - see https://bugs.php.net/bug.php?id=44223 . Apparently it's a feature.
To work around this "feature" when converting to a string, surround your round statement with an sprintf:
sprintf("%.10f", round( $amountToBeRounded, 10));
php at silisoftware dot com ¶
10 years ago
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:
<?php
round(1241757, -3); // 1242000
RoundSigDigs(1241757, 3); // 1240000
?>
Works on negative numbers too. $sigdigs should be >= 0
<?php
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;
}
?>
feha at vision dot to ¶
2 years ago
Here is a short neat function to round minutes (hour) ...
<?php
function minutes_round ($hour = '14:03:32', $minutes = '5', $format = "H:i")
{
// by Femi Hasani [www.vision.to]
$seconds = strtotime($hour);
$rounded = round($seconds / ($minutes * 60)) * ($minutes * 60);
return date($format, $rounded);
}
?>
You decide to round to nearest minute ...
example will produce : 14:05
tichoux at charlevoix dot net ¶
10 years ago
for a poll, if you want to have 100% and not 99 or 99.99 % you can do that :
<?php
round( number_format( (($individual_result*100)/$total_result), 2), 1);
?>
twan at ecreation dot nl ¶
13 years ago
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:
<?php printf ("%6.2f",3.39532); ?>
This returns: 3.40 .
omnibus at omnibus dot edu dot pl ¶
2 years ago
Beware strange behaviour if number is negative and precision is bigger than the actual number of digits after comma.
round(-0.07, 4);
returns
-0.07000000000000001
So if you validate it against a regular expression requiring the maximum amount of digits after comma, you'll get into trouble.
christian at deligant dot net ¶
1 year ago
this function (as all mathematical operators) takes care of the setlocale setting, resulting in some weirdness when using the result where the english math notation is expected, as the printout of the result in a width: style attribute!
<?php
$a=3/4;
echo round($a, 2); // 0.75
setlocale(LC_ALL, 'it_IT@euro', 'it_IT', 'it');
$b=3/4;
echo round($b,2); // 0,75
?>
Anonymous ¶
3 years ago
This functions return ceil($nb) if the double or float value is bigger than "$nb.5" else it's return floor($nb)
<?php
function arounds_int($nb) {
if(!is_numeric($nb)) {
return false;
}
$sup = round($nb);
$inf = floor($nb);
$try = (double) $inf . '.5' ;
if($nb > $try) {
return $sup;
}
return $inf;
}
?>
michaeldnelson dot mdn at gmail dot com ¶
3 years ago
This function will let you round to an arbitrary non-zero number. Zero of course causes a division by zero.
<?php
function roundTo($number, $to){
return round($number/$to, 0)* $to;
}
echo roundTo(87.23, 20); //80
echo roundTo(-87.23, 20); //-80
echo roundTo(87.23, .25); //87.25
echo roundTo(.23, .25); //.25
?>
chris at chrisstockton dot org ¶
4 years ago
In case anyone has a problem like me ever, were you are doing very large stat calculations on a array and end up with floats with way to large of precision. A good way to round them all to N length is below.
<?php
public function recursiveRound(array &$arr, $precision)
{
foreach($arr as $key => $val) {
if(is_array($val)) {
$this->recursiveRound($arr[$key], $precision);
} elseif(is_float($val)) {
$arr[$key] = round($arr[$key], $precision);
}
}
return $arr;
}
?>
martinr at maarja dot net ¶
5 years ago
Please note that the format of this functions output also depends on your locale settings. For example, if you have set your locale to some country that uses commas to separate decimal places, the output of this function also uses commas instead of dots.
This might be a problem when you are feeding the rounded float number into a database, which requires you to separate decimal places with dots.
See it in action:
<?php
echo round('3.5558', 2);
setlocale(constant('LC_ALL'), 'et_EE.UTF-8');
echo '<br />'. round('3.5558', 2);
?>
The output will be:
3.56
3,56
tom at crysis-online dot com ¶
6 years ago
I just found out then that even if you round a double (3.7) to an integer (4), it's data type remains as 'double'. So it's always good to use the settype() function when using the round() function to prevent any problems with your scripts.
maxteiber at gmail dot com ¶
6 years ago
the result of this function always depends on the underlying C function. There have been a lot of compiler bugs and floating-point precission problems involving this function. Right now the following code:
<?php
echo round(141.075, 2);
?>
returns:
141.07
on my machine.
So never really trust this function when you do critical calculations like accounting stuff!
Instead: use only integers or use string comparisons.
ianring (at) golden.net ¶
9 years ago
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.
<?php
$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:
<?php
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 ¶
10 years ago
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:
<?php
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;
}
?>
Bevan ¶
3 years ago
Formats a number to the specified number of significant figures.
<?php
/**
* Formats numbers to the specified number of significant figures.
*
* @author Bevan Rudge, Drupal.geek.nz
*
* @param number $number
* The number to format.
* @param integer $sf
* The number of significant figures to round and format the number to.
* @return string
* The rounded and formatted number.
*/
function format_number_significant_figures($number, $sf) {
// How many decimal places do we round and format to?
// @note May be negative.
$dp = floor($sf - log10(abs($number)));
// Round as a regular number.
$number = round($number, $dp);
// Leave the formatting to format_number(), but always format 0 to 0dp.
return number_format($number, 0 == $number ? 0 : $dp);
}
?>
mydanielsmile+phpnet at gmail dot com ¶
4 years ago
Function to round in increments I made:
<?php
function round_to($number, $increments) {
$increments = 1 / $increments;
return (round($number * $increments) / $increments);
}
?>
For example:
<?php
$n = 5.3;
echo round_to($n, 0.5); // 5.5
?>
Daniel.
Chris ¶
4 years ago
I dont think it is a bug as such but when rounding genrally you tend to only round from the digit after the precision you want.
The only way to get arround this is by rounding it twice with diffrent precisions:
<?php
$round1 = round(8.1246, 3); //8.125
$round2 = round($round1, 2); //8.13
echo $round2;
?>
lossantis at ig dot com dot br ¶
2 years ago
Since the mode parameter for options like PHP_ROUND_HALF_UP is available as of PHP 5.3, here is an alternative for ceiling:
<?php echo 252 / 40; // 6.3 ?>
If I round this:
<?php echo round(252 / 40); // 6 ?>
You can also use a ceil (which might be useful for pagination):
<?php echo ceil(252/40); // 7 ?>
[Edited by: googleguy@php.net for clarity]
Anonymous ¶
3 months ago
Note that the returned string does not always include as many decimal digits as required by the precision argument.
It is because the least significant decimal digit(s) is (are) not reported if it is (they are) 0.
So:
<?php
round(1.105,2); // returns 1.11
?>
but:
<?php
round(1.104,2); // returns 1.1 instead of 1.10
?>
More:
<?php
round(1.004,2); // returns 1 (even the decimal point is not reported)
?>
hugues at zonereseau dot com ¶
3 years ago
I had problem with round() function I didn't gave me the same result in windows or on a linux server :
<?php
round(4.725, 2); // gave me 4.72 on linux
round(4.725, 2); // gave me 4.73 on windows
?>
The expected result was 4.73
Here my function to resolve my problem
<?php
function mround($number, $precision=0) {
$precision = ($precision == 0 ? 1 : $precision);
$pow = pow(10, $precision);
$ceil = ceil($number * $pow)/$pow;
$floor = floor($number * $pow)/$pow;
$pow = pow(10, $precision+1);
$diffCeil = $pow*($ceil-$number);
$diffFloor = $pow*($number-$floor)+($number < 0 ? -1 : 1);
if($diffCeil >= $diffFloor) return $floor;
else return $ceil;
}
echo mround(4.725, 2); // Yes 4.73
?>
