php怎么判断有几位小数
- 建站笔记
- 2022-05-10
- 581
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); ?>
输出结果:
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
本文由白琉璃于2022-05-10发表在白琉璃源码网,如有侵权或疑问,请联系我们,谢谢。
本文链接:https://www.bailiuli.com/t/2927.html
发表评论