Php obtains the nearest number in a specified range. php obtains the _ PHP Tutorial.

Source: Internet
Author: User
Php obtains the nearest number in the specified range. Php obtains the nearest number in a specified range. php obtains the instance in this article and describes how php obtains the nearest number in a specified range. Share it with you for your reference. The specific implementation method is as follows: php obtains the nearest number in a specified range. php obtains

This example describes how to obtain the nearest number in a specified range by php. Share it with you for your reference. The specific 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 10 20 30 40 50 ...print 'NextRelatedNumber: ';print NextRelatedNumber(44, 10) . "\n";// returns --> 40// 0 20 40 60 80 100 ...print 'NextHigherNumber: ';print NextHigherNumber(41, 20) . "\n";// returns --> 60// 0 5 10 15 20 25 30 35 ...print 'NextLowerNumber: ';print NextLowerNumber(57, 5) . "\n";// returns --> 55// Example with Arrayprint 'NextNumberArray: ';print NextNumberArray(45, array(3, 8, 19, 34, 56, 89)) . "\n";// returns --> 34// (45 is between 34 and 56 but 34 is the next)

I hope this article will help you with php programming.

Examples in this article describes how to obtain the nearest number in a specified range by php. Share it with you for your reference. The specific 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.