Home >Backend Development >PHP Tutorial >PHP remainder function code_PHP tutorial

PHP remainder function code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:21:29881browse

Copy code The code is as follows:

//Get the numerical value in the base digit
function getRemainder($num, $bin, $pos, &$result = 0){
//author lianq.net
//$num value, decimal
//$bin the base to be converted
//$pos digits
$real_len = log($num, $bin);//Logarithm, find the length of the original value
$floor_len = floor($real_len);//Tear off and round up
$base = pow( $bin, $pos-1);//Base
$divisor = pow($bin,$pos);//Divisor
if($num >= $divisor){
$new_num = $num % pow($bin, $floor_len);
getRemainder($new_num, $bin, $pos, $result);
}else{
$result = floor($num / $base) ;
}
return $result;
}

//For example, when the value 16 is converted to base 9, what is the first digit of it?
$a = getRemainder(16,9, 1);
echo $a;//Output 7

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324912.htmlTechArticleCopy code The code is as follows: //Get the numerical value in the base digit function getRemainder($num, $bin, $ pos, //Logarithm, find the length of the original value $floor_len = floor($real_len);//Trop it out to find the integer $base = pow($bin...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn