php怎么判断有几位小数

来自:互联网
时间:2021-10-20
阅读:

php判断一个数有几位小数的方法

方法1:借助strrpos()、substr()、strlen()函数

<?php
function getLen($num)
{

         $pos = strrpos($num, '.');
         $ext = substr($num, $pos+1);
	 $len=strlen($ext);
         return $len;
}
$num=3.14254;
echo getLen($num);
?>

输出结果:

5

方法2:借助explod()、array_pop()、strlen()函数

<?php
function getLen($num)
{
         $arr = explode('.',$num);
	 $str=array_pop($arr);
	 $len=strlen($str);
         return $len;
}
$num=25.365;
echo getLen($num);
?>

输出结果:

3
返回顶部
顶部