round

(PHP 4, PHP 5)

roundRounds 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_UP Round val up to precision decimal places away from zero, when it is half way there. Making 1.5 into 2 and -1.5 into -2.
PHP_ROUND_HALF_DOWN Round val down to precision decimal places towards zero, when it is half way there. Making 1.5 into 1 and -1.5 into -1.
PHP_ROUND_HALF_EVEN Round val to precision decimal places towards the next even value.
PHP_ROUND_HALF_ODD Round val to precision decimal 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.60);      // 4
echo round(1.955832);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.0452);    // 5.05
echo round(5.0552);    // 5.06
?>

Example #2 mode examples

<?php
echo round(9.50PHP_ROUND_HALF_UP);   // 10
echo round(9.50PHP_ROUND_HALF_DOWN); // 9
echo round(9.50PHP_ROUND_HALF_EVEN); // 10
echo round(9.50PHP_ROUND_HALF_ODD);  // 9

echo round(8.50PHP_ROUND_HALF_UP);   // 9
echo round(8.50PHP_ROUND_HALF_DOWN); // 8
echo round(8.50PHP_ROUND_HALF_EVEN); // 8
echo round(8.50PHP_ROUND_HALF_ODD);  // 9
?>

Example #3 mode with precision examples

<?php
/* Using PHP_ROUND_HALF_UP with 1 decimal digit precision */
echo round1.551PHP_ROUND_HALF_UP);   //  1.6
echo round1.541PHP_ROUND_HALF_UP);   //  1.5
echo round(-1.551PHP_ROUND_HALF_UP);   // -1.6
echo round(-1.541PHP_ROUND_HALF_UP);   // -1.5

/* Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision */
echo round1.551PHP_ROUND_HALF_DOWN); //  1.5
echo round1.541PHP_ROUND_HALF_DOWN); //  1.5
echo round(-1.551PHP_ROUND_HALF_DOWN); // -1.5
echo round(-1.541PHP_ROUND_HALF_DOWN); // -1.5

/* Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision */
echo round1.551PHP_ROUND_HALF_EVEN); //  1.6
echo round1.541PHP_ROUND_HALF_EVEN); //  1.5
echo round(-1.551PHP_ROUND_HALF_EVEN); // -1.6
echo round(-1.541PHP_ROUND_HALF_EVEN); // -1.5

/* Using PHP_ROUND_HALF_ODD with 1 decimal digit precision */
echo round1.551PHP_ROUND_HALF_ODD);  //  1.5
echo round1.541PHP_ROUND_HALF_ODD);  //  1.5
echo round(-1.551PHP_ROUND_HALF_ODD);  // -1.5
echo round(-1.541PHP_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

Коментарии

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 .
2000-05-15 21:51:00
http://php5.kiev.ua/manual/ru/function.round.html
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(12417573); // 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;
}
?>
2002-08-14 17:15:13
http://php5.kiev.ua/manual/ru/function.round.html
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/$to0)* $to;
}

echo 
roundTo(87.2320); //80
echo roundTo(-87.2320); //-80
echo roundTo(87.23.25); //87.25
echo roundTo(.23.25); //.25
?>
2009-09-25 09:42:12
http://php5.kiev.ua/manual/ru/function.round.html
Автор:
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 $increments;
    return (
ceil($number $increments) / $increments);
}
?>
2010-10-07 19:07:58
http://php5.kiev.ua/manual/ru/function.round.html
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($a2); // 0.75

setlocale(LC_ALL'it_IT@euro''it_IT''it');
$b=3/4;
echo 
round($b,2); // 0,75
?>
2011-09-15 04:24:11
http://php5.kiev.ua/manual/ru/function.round.html
Автор:
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));
2012-07-12 14:16:41
http://php5.kiev.ua/manual/ru/function.round.html
function mround($val, $f=2, $d=6){
    return sprintf("%".$d.".".$f."f", $val);
}

echo mround(34.89999);  //34.90
2013-12-17 15:34:47
http://php5.kiev.ua/manual/ru/function.round.html
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.
2014-01-13 13:28:19
http://php5.kiev.ua/manual/ru/function.round.html
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);
}
?>
2014-03-07 17:00:21
http://php5.kiev.ua/manual/ru/function.round.html
Unexpected result or misunderstanding (php v5.5.9)

<?php

echo round(1.551PHP_ROUND_HALF_DOWN); // 1.5
echo round(1.5511PHP_ROUND_HALF_DOWN); //1.6

?>
2014-07-02 01:49:37
http://php5.kiev.ua/manual/ru/function.round.html
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)
2014-09-02 06:34:49
http://php5.kiev.ua/manual/ru/function.round.html
/**
 * 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
 * 
 */
2016-11-28 23:25:25
http://php5.kiev.ua/manual/ru/function.round.html
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($val0$p $f));
    }
    return 
$val;
}
?>

Originally posted in http://stackoverflow.com/a/12710283/1596489
2017-01-13 19:51:17
http://php5.kiev.ua/manual/ru/function.round.html

    Поддержать сайт на родительском проекте КГБ