Monday 2 February 2015

Rounding off variable values in PHP

Rounding off variable values in PHP

We can round off variable values to a whole number or to some value with precision. You can read the article on number formatting to know how to format numbers using PHP. For simple rounding here is the code to do all that. WE will store the value in a variable and then display by rounding off it by using round function.

Sample code for rounding data below

$number=25.66745;
echo "The number = ". $number;               // will display 25.66745
echo "<br>Rounded value of the number = ".round($number,2);     // will display 25.67 
echo "<br>Rounded value of the number = ".round($number);     // will display 26

Some time we may use php number formatting function to display a proper formatted number. 

We can round off the number to lower value is by using floor function and we can also round off to next higher number by using ceil function in PHP

0 comments:

Post a Comment