Home > php教程 > php手册 > PHP取进制余数函数代码

PHP取进制余数函数代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 12:02:50
Original
1160 people have browsed it

复制代码 代码如下:


//取进制位上的数值
function getRemainder($num, $bin, $pos, &$result = 0){
//author lianq.net
//$num 数值,十进制
//$bin 要转换的进制
//$pos 位数
$real_len = log($num, $bin);//对数,求原值长度
$floor_len = floor($real_len);//舍去求整
$base = pow($bin, $pos-1);//基数
$divisor = pow($bin,$pos);//除数
if($num >= $divisor){
$new_num = $num % pow($bin, $floor_len);
getRemainder($new_num, $bin, $pos, $result);
}else{
$result = floor($num / $base);
}
return $result;
}

//比如,数值16转换为9进制时,它的第一位上的数值是多少?
$a = getRemainder(16,9, 1);
echo $a;//输出7

source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template