php判断有几位小数的方法:1、使用“strlen(substr($num, strrpos($num, '.')+1))”语句判断;2、使用“strlen(array_pop(explode('.',$num)))”语句。

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑
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);
?>输出结果:
立即学习“PHP免费学习笔记(深入)”;
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
推荐学习:《PHP视频教程》
以上就是php怎么判断有几位小数的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号