PHP gets the closest number of methods in the specified range, and PHP gets
This example describes the method by which PHP obtains the nearest number within a specified range. Share to everyone for your reference. The implementation method is as follows:
Returns the next higher or lower numberfunction nextrelatednumber ($number, $range) {$r = $number% $range; $f = $number-$r; $b = Round ($r/$range, 0); return ($b = = 1)? $f + $range: $f; }//Returns the next higher numberfunction nexthighernumber ($number, $range) {$r = $number% $range; $f = $number-$r; $b = Ceil ($r/$range); return ($b = = 1)? $f + $range: $f; }//Returns the next lower numberfunction Nextlowernumber ($number, $range) {$r = $number% $range; $f = $number-$r; $b = Floor ($r/$range); return ($b = = 1)? $f + $range: $f; }//Returns the next related number from an arrayfunction Nextnumberarray ($Number, $NumberRangeArray) {$w = 0; $c =-1; $abstand = 0; $l = count ($NumberRangeArray); for ($pos =0; $pos < $l; $pos + +) {$n = $NumberRangeArray [$pos]; $abstand = ($n < $Number)? $Number-$n: $n-$Number; if ($c = =-1) {$c = $abstand; Continue } else if ($abstand < $c) {$c = $abstand; $w =$pos; }} return $NumberRangeArray [$w];} examples//--------//0 ... print ' nextrelatednumber: ';p rint nextrelatednumber (44, 10). "\ n";//returns---40//0 ... print ' nexthighernumber: ';p rint nexthighernumber (41, 20). "\ n";//Returns--60//0 5 ... print ' Nextlowernumber: ';p rint nextlowernumber (57, 5). "\ n";//returns--55//Example with Arrayprint ' Nextnumberarray: ';p rint Nextnumberarray (3, 8, 19, 34, 56, 89)). "\ n";//Returns-34//(is between and all but the next)
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1011252.html www.bkjia.com true http://www.bkjia.com/PHPjc/1011252.html techarticle PHP gets the closest number of methods in the specified range, and PHP gets the examples in this article about how PHP gets the nearest number in a specified range. Share to everyone for your reference. The concrete implementation method is as follows ...