PHP round

PHP round() function is used to round a floating point number.

Syntax :

round(number,precision,mode);

Parameter,

number : Required. it is floating number.
precision : Optional. It specifies number of digits after the decimal point. Default is 0
mode : Optional. Below constant to specify the rounding mode in which rounding occurs.

  •  PHP_ROUND_HALF_UP :  Rounds number away from zero when it is half way there, making 5.5 into 6 and -5.5 into -6.
  •  PHP_ROUND_HALF_DOWN : Rounds number towards zero when it is half way there, making 5.5 into 5 and -5.5 into -5.
  •  PHP_ROUND_HALF_EVEN : Rounds number towards the nearest even value when it is half way there, making both 5.5 and 6.5 into 6.
  • PHP_ROUND_HALF_ODD : Rounds number towards the nearest odd value when it is half way there, making 5.5 into 5 and and 5.5 into 6.

           

Example :

<?php
//code by aryatechno
echo "<br> The round number for 0.45 is ".round(0.45);
echo "<br> The round number for 0.85 is ".round(0.85);
echo "<br> The round number for -3.65 is ".round(-3.65);
echo "<br> The round number for -7.15 is ".round(-7.15);
echo "<br> The round number for -12.87 is ".round(-12.87);
//using mode with round() function
echo "<br> The round number with mode for 11.50 is ".round(11.50, 0, PHP_ROUND_HALF_UP);
echo "<br> The round number with mode for 11.50 is ".round(11.50, 0, PHP_ROUND_HALF_DOWN);
echo "<br> The round number with mode for 11.50 is ".round(11.50, 0, PHP_ROUND_HALF_EVEN);
echo "<br> The round number with mode for 11.50 is ".round(11.50, 0, PHP_ROUND_HALF_ODD);
?>

Output :

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

77981