Monday 2 February 2015

Getting the higher and lower hundred value using ceil and floor function in PHP

Getting the higher and lower hundred value using ceil and floor function in PHP

IN an umber we can find out the next higher integer using ceil function. This function is different than round function. In case of round function the value is rounded to higher or lower value, but here it is always higher value is returned. Here is an example
$v = ceil(6.25);
// which would make $v=7
This function is useful when we search for a next higher hundred or thousand value of a number. For example we have a number 785 and wants to know what is the next higher hundredth number ( that is 800 ). To know this we can use ceil function. If you are generating a graph and coordinates are fixed according to high and low value of the data then using this function we can find out what should be our highest point in the graph.
$v=485;
$base_m=100;
$v_max=$base_m*(ceil($v/$base_m));
//$v_max equal to 500
Same way we can find out the lowest value of a figure using floor function in php
$v=485;
$base_m=100;
$v_min=$base_m*(floor($v/$base_m));
//$v_min equal to 400
Note that there is no point is using ceil and floor functions on integers directly. ( we will get the same value )

0 comments:

Post a Comment