PHP 8.0.0 Beta 4 available for testing

round

(PHP 4, PHP 5, PHP 7)

roundОкругляет число типа float

Описание

round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] ) : float

Возвращает округлённое значение val с указанной точностью precision (количество цифр после запятой). precision может быть отрицательным или нулём (по умолчанию).

Замечание: PHP по умолчанию не может правильно обрабатывать строки типа "12,300.2". Более подробно читайте в разделе Преобразование строк в числа.

Список параметров

val

Значение для округления.

precision

Количество десятичных знаков, до которых производится округление

Если precision положительный, val округляется до точности precision цифр после запятой.

Если precision отрицателен, val округляется до точности precision цифр перед десятичной запятой, то есть до ближайшего кратного pow(10, -precision), например для precision равной -1 val округляется до десятков, для precision равной -2 до сотен и т.д.

mode

Используйте одну из этих констант для задания способа округления.

Константы Описание
PHP_ROUND_HALF_UP Округляет val от нуля, когда следующий знак находится посередине. То есть округляет 1.5 в 2 и -1.5 в -2.
PHP_ROUND_HALF_DOWN Округляет val к нулю, когда следующий знак находится посередине. То есть округляет 1.5 в 1 и -1.5 в -1.
PHP_ROUND_HALF_EVEN Округляет val до ближайшего четного значения, когда следующий знак находится посередине. То есть округляет 1.5 и 2.5 в 2.
PHP_ROUND_HALF_ODD Округляет val до ближайшего нечетного значения, когда следующий знак находится посередине. То есть округляет 1.5 и 2.5 в 3.

Возвращаемые значения

Значение округляется до заданного значения precision как float.

Примеры

Пример #1 Пример использования round()

<?php
var_dump
(round(3.4));
var_dump(round(3.5));
var_dump(round(3.6));
var_dump(round(3.60));
var_dump(round(5.0452));
var_dump(round(5.0552));
var_dump(round(345, -2));
var_dump(round(345, -3));
var_dump(round(678, -2));
var_dump(round(678, -3));
?>

Результат выполнения данного примера:

float(3)
float(4)
float(4)
float(4)
float(5.05)
float(5.06)
float(300)
float(0)
float(700)
float(1000)

Пример #2 Как параметр precision влияет на числа с плавющей запятой

<?php
$number 
135.79;

var_dump(round($number3));
var_dump(round($number2));
var_dump(round($number1));
var_dump(round($number0));
var_dump(round($number, -1));
var_dump(round($number, -2));
var_dump(round($number, -3));
?>

Результат выполнения данного примера:

float(135.79)
float(135.79)
float(135.8)
float(136)
float(140)
float(100)
float(0)

Пример #3 Примеры использования параметра mode

<?php
echo 'Режимы округления с 9.5' PHP_EOL;
var_dump(round(9.50PHP_ROUND_HALF_UP));
var_dump(round(9.50PHP_ROUND_HALF_DOWN));
var_dump(round(9.50PHP_ROUND_HALF_EVEN));
var_dump(round(9.50PHP_ROUND_HALF_ODD));

echo 
PHP_EOL;
echo 
'Режимы округления с 8.5' PHP_EOL;
var_dump(round(8.50PHP_ROUND_HALF_UP));
var_dump(round(8.50PHP_ROUND_HALF_DOWN));
var_dump(round(8.50PHP_ROUND_HALF_EVEN));
var_dump(round(8.50PHP_ROUND_HALF_ODD));
?>

Результат выполнения данного примера:

Режимы округления с 9.5
float(10)
float(9)
float(10)
float(9)

Режимы округления с 8.5
float(9)
float(8)
float(8)
float(9)

Пример #4 Примеры использования параметра mode с указанием точности precision

<?php
echo 'Использование PHP_ROUND_HALF_UP с точностью до 1 знака' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_UP));
var_dump(round(-1.551PHP_ROUND_HALF_UP));

echo 
PHP_EOL;
echo 
'Использование PHP_ROUND_HALF_DOWN с точностью до 1 знака' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_DOWN));
var_dump(round(-1.551PHP_ROUND_HALF_DOWN));

echo 
PHP_EOL;
echo 
'Использование PHP_ROUND_HALF_EVEN с точностью до 1 знака' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_EVEN));
var_dump(round(-1.551PHP_ROUND_HALF_EVEN));

echo 
PHP_EOL;
echo 
'Использование PHP_ROUND_HALF_ODD с точностью до 1 знака' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_ODD));
var_dump(round(-1.551PHP_ROUND_HALF_ODD));
?>

Результат выполнения данного примера:

Использование PHP_ROUND_HALF_UP с точностью до 1 знака
float(1.6)
float(-1.6)

Использование PHP_ROUND_HALF_DOWN с точностью до 1 знака
float(1.5)
float(-1.5)

Использование PHP_ROUND_HALF_EVEN с точностью до 1 знака
float(1.6)
float(-1.6)

Использование PHP_ROUND_HALF_ODD с точностью до 1 знака
float(1.5)
float(-1.5)

Список изменений

Версия Описание
5.3.0 Был добавлен параметр mode.
5.2.7 Работа функции round() была изменена в соответствии со стандартом C99.

Смотрите также

  • ceil() - Округляет дробь в большую сторону
  • floor() - Округляет дробь в меньшую сторону
  • number_format() - Форматирует число с разделением групп

add a note add a note

User Contributed Notes 32 notes

up
271
takingsides at gmail dot com
6 years ago
In my opinion this function lacks two flags:

- PHP_ROUND_UP - Always round up.
- PHP_ROUND_DOWN - Always round down.

In accounting, it's often necessary to always round up, or down to a precision of thousandths.

<?php
function round_up($number, $precision = 2)
{
   
$fig = (int) str_pad('1', $precision, '0');
    return (
ceil($number * $fig) / $fig);
}

function
round_down($number, $precision = 2)
{
   
$fig = (int) str_pad('1', $precision, '0');
    return (
floor($number * $fig) / $fig);
}
?>
up
3
Mojo urk
2 years ago
Solving round_down() problem:
-----------------------------
Use of <?php floor(pow(10, $precision) * $value) / pow(10, $precision); ?> fails in some cases, e.g. round_down(2.05, 2) gives incorrect 2.04.
Here is a "string" solution (https://stackoverflow.com/a/26491492/1245149) of the problem (a negative precision is not covered):

<?php
function round_down($value, $precision) {       
   
$value = (float)$value;
   
$precision = (int)$precision;
    if (
$precision < 0) {
       
$precision = 0;
    }
   
$decPointPosition = strpos($value, '.');
    if (
$decPointPosition === false) {
        return
$value;
    }
    return (float)(
substr($value, 0, $decPointPosition + $precision + 1));       
}
?>

Solving round_up() problem:
---------------------------
Use of <?php ceil(pow(10, $precision) * $value) / pow(10, $precision);?> fails in some cases, e.g. round_up(2.22, 2) gives incorrect 2.23 (https://stackoverflow.com/a/8239620/1245149).
Adapting the above round_down() "string" solution I have got this result (a negative precision is not covered):

<?php
function round_up($value, $precision) {       
   
$value = (float)$value;
   
$precision = (int)$precision;
    if (
$precision < 0) {
       
$precision = 0;
    }
   
$decPointPosition = strpos($value, '.');
    if (
$decPointPosition === false) {
        return
$value;
    }
   
$floorValue = (float)(substr($value, 0, $decPointPosition + $precision + 1));
   
$followingDecimals = (int)substr($value, $decPointPosition + $precision + 1);
    if (
$followingDecimals) {
       
$ceilValue = $floorValue + pow(10, -$precision); // does this give always right result?
   
}
    else {
       
$ceilValue = $floorValue;
    }
    return
$ceilValue;               
}
?>

I don't know it is bulletproof, but at least it removes the above mentioned fail. I have done no binary-to-decimal-math-analysis but if `$floorValue + pow(10, 0 - $precision)` works
always as expected then it should be ok.
up
23
depaula at unilogica dot com
3 years ago
As PHP doesn't have a a native number truncate function, this is my solution - a function that can be usefull if you need truncate instead round a number.

<?php
/**
* Truncate a float number, example: <code>truncate(-1.49999, 2); // returns -1.49
* truncate(.49999, 3); // returns 0.499
* </code>
* @param float $val Float number to be truncate
* @param int f Number of precision
* @return float
*/
function truncate($val, $f="0")
{
    if((
$p = strpos($val, '.')) !== false) {
       
$val = floatval(substr($val, 0, $p + 1 + $f));
    }
    return
$val;
}
?>

Originally posted in http://stackoverflow.com/a/12710283/1596489
up
24
slimusgm at gmail dot com
6 years ago
If you have negative zero and you need return positive number simple add +0:

$number = -2.38419e-07;
var_dump(round($number,1));//float(-0)
var_dump(round($number,1) + 0);//float(0)
up
21
djcox99 at googlemail dot com
6 years ago
I discovered that under some conditions you can get rounding errors with round when converting the number to a string afterwards.

To fix this I swapped round() for number_format().

Unfortunately i cant give an example (because the number cant be represented as a string !)

essentially I had round(0.688888889,2);

which would stay as 0.68888889 when printed as a string.

But using number_format it correctly became 0.69.
up
7
jongbumi at gmail dot com
4 years ago
PHP 5.3, 5.4, 5.5
<?php
$fInfinty
= pow(1000, 1000); // float(INF)
$fResult = round(123.456, $fInfinty); // double(123)
?>

PHP 5.6
<?php
$fInfinty
= pow(1000, 1000); // float(INF)
$fResult = round(123.456, $fInfinty); // float(0)
?>

PHP 7
<?php
$fInfinty
= pow(1000, 1000); // float(INF)
$fResult = round(123.456, $fInfinty); // null
?>
up
13
twan at ecreation dot nl
20 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 .
up
3
serg at kalachev dot ru
6 years ago
Excel-like ROUNDUP function:

public static function round_up($value, $places)
{
    $mult = pow(10, abs($places));
     return $places < 0 ?
    ceil($value / $mult) * $mult :
        ceil($value * $mult) / $mult;
}

echo round_up(12345.23, 1); // 12345.3
echo round_up(12345.23, 0); // 12346
echo round_up(12345.23, -1); // 12350
echo round_up(12345.23, -2); // 12400
echo round_up(12345.23, -3); // 13000
echo round_up(12345.23, -4); // 20000
up
11
Anonymous
9 years ago
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);
}
?>
up
5
christian at deligant dot net
9 years 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
?>
up
9
php at silisoftware dot com
18 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;
}
?>
up
4
michaeldnelson dot mdn at gmail dot com
10 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
?>
up
7
esion99 at gmail dot com
6 years ago
Unexpected result or misunderstanding (php v5.5.9)

<?php

echo round(1.55, 1, PHP_ROUND_HALF_DOWN); // 1.5
echo round(1.551, 1, PHP_ROUND_HALF_DOWN); //1.6

?>
up
4
craft at ckdevelop dot org
6 years ago
function mround($val, $f=2, $d=6){
    return sprintf("%".$d.".".$f."f", $val);
}

echo mround(34.89999);  //34.90
up
6
martinr at maarja dot net
12 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
up
1
dastra
8 years 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));
up
2
goreyshi at gmail dot com
2 years ago
When you have a deal with money like dollars, you need to display it under this condition:
-format all number with two digit decimal for cents.
-divide 1000 by ,
-round half down for number with more than two decimal

I approach it using round function inside the number_format function:

number_format((float)round( 625.371 ,2, PHP_ROUND_HALF_DOWN),2,'.',',')  // 625.37
number_format((float)round( 625.379 ,2, PHP_ROUND_HALF_DOWN),2,'.',',')  // 625.38
number_format((float)round( 1211.20 ,2, PHP_ROUND_HALF_DOWN),2,'.',',')  // 1,211.20
number_format((float)round( 625 ,2, PHP_ROUND_HALF_DOWN),2,'.',',')      // 625.00
up
0
Astro
6 months ago
Okay, final version of my function:

function NumberPrecision($n, $precision=0, $is_round=true)
            {
            if ($is_round)
                {
                $r = 5 * pow(10, -($precision+1));
                $n += (($n < 0) ? -$r : $r);
                }
           
            $comma = '.';
           
            $r = 5 * pow(10, -($precision+2));
            $n += (($n > 0) ? -$r : $r);
            $n = number_format($n, $precision+1, $comma, '');
           
            $n .= $comma;
            list($n, $frac) = explode($comma, $n, 2);
            $n = rtrim(rtrim($n, $comma) . $comma . substr($frac, 0, $precision), $comma);
            return ($n);
            }

It can be useful in come cases when built-in function like round() or number_format() returns unexpected results. Works with positive and negative numbers, zero, numbers like 1/12, 0.3, numbers in scientific notation etc.
up
0
Hayley Watson
1 year ago
It should just be noted that what is called "precision" on this page is more correctly called accuracy; precision is the total number of significant digits on both sides of the decimal point, while accuracy is the number of digits to the right of the point. It's a common confusion.
up
1
Anonymous
3 years ago
Note that PHP 5.3 didn't just introduce $mode, it rewrote the rounding implementation completely to eliminate many kinds of rounding errors common to rounding floating point values.

That's why round() gives you the correct result even when floor/ceil don't.
For example,  floor(0.285 * 100 + 0.5) VS round(0.285*100 + 0.5). First one gives 28, second one gives 29.

More details here: https://wiki.php.net/rfc/rounding
up
0
greghenle at gmail dot com
3 years ago
/**
* Round to first significant digit
* +N to +infinity
* -N to -infinity
*
*/
function round1stSignificant ( $N ) {
  if ( $N === 0 ) {
    return 0;
  }

  $x = floor ( log10 ( abs( $N ) ) );

  return ( $N > 0 )
    ? ceil( $N * pow ( 10, $x * -1 ) ) * pow( 10, $x )
    : floor( $N * pow ( 10, $x * -1 ) ) * pow( 10, $x );
}

echo round1stSignificant( 39144818 ) . PHP_EOL;
echo round1stSignificant( 124818 ) . PHP_EOL;
echo round1stSignificant( 0.07468 ) . PHP_EOL;
echo round1stSignificant( 0 ) . PHP_EOL;
echo round1stSignificant( -0.07468 ) . PHP_EOL;

/**
* Output
*
* 40000000
* 200000
* 0.08
* 0
* -0.08
*
*/
up
-1
feha at vision dot to
10 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
up
-2
spectrumcat at gmail dot com
6 years ago
In case someone will need a "graceful" rounding (that changes it's precision to get a non 0 value) here's a simple function:

function gracefulRound($val, $min = 2, $max = 4) {
    $result = round($val, $min);
    if ($result == 0 && $min < $max) {
        return gracefulRound($val, ++$min, $max);
    } else {
        return $result;
    }
}

Usage:
$_ = array(0.5, 0.023, 0.008, 0.0007, 0.000079, 0.0000048);
foreach ($_ as $val) {
    echo "{$val}: ".gracefulRound($val)."\n";
}

Output:
0.5: 0.5
0.023: 0.02
0.008: 0.01
0.0007: 0.001
0.000079: 0.0001
0.0000048: 0
up
-2
Anonymous
10 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;
    }
?>
up
-4
omnibus at omnibus dot edu dot pl
9 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.
up
-2
terry at scribendi dot com
16 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'.
up
-4
Anonymous
3 years ago
This function has strange. behaviors:

<?php
echo round(0.045, 2);      // 0.05
echo round(0.45, 1);      // 0.5
echo round(1.045-1, 2);    // 0.04 !!!
echo round(1.45-1, 1);      // 0.5
up
-2
php at persignum dot com
5 years ago
Because this function is missing round up and round down constants and the top note doesn't really show you how to round up or down to the nearest number, here is an easy way to always round up or always round down to the nearest number.

int is the number you want to round

n is the nearest number you want rounded to.

Round up to the nearest number

function round_up($int, $n) {
    return ceil($int / $n) * $n;
}

And to round down to the nearest number

function round_down(int, $n) {
    return floor($int / $n) * $n;
}
up
-5
maxteiber at gmail dot com
13 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.
up
-6
lossantis at ig dot com dot br
10 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]
up
-10
Bevan
11 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);
}
?>
up
-5
armanhakimsagar at gmail dot com
3 years ago
$a = .9;

    $b = .8;

    $d = .1;

    $e = .2;

    $x = $a-$b;

    $y = $e-$d;

    $f = round($x,2);

    echo $f;

    if($f==$y){

        echo "ok";
    }
To Top