PHP gets _php tutorial for getting the nearest number within a specified range

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.