Home >Backend Development >PHP Problem >How to determine how many decimals there are in php
How to determine how many decimals there are in php: 1. Use the "strlen(substr($num, strrpos($num, '.') 1))" statement to determine; 2. Use "strlen(array_pop( explode('.',$num)))" statement.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php determines a number Methods for how many decimals there are
Method 1: Use strrpos(), substr(), strlen() functions
<?php function getLen($num) { $pos = strrpos($num, '.'); $ext = substr($num, $pos+1); $len=strlen($ext); return $len; } $num=3.14254; echo getLen($num); ?>
Output results:
5
Method 2: Use the explode(), array_pop(), strlen() functions
<?php function getLen($num) { $arr = explode('.',$num); $str=array_pop($arr); $len=strlen($str); return $len; } $num=25.365; echo getLen($num); ?>
Output result:
3
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine how many decimals there are in php. For more information, please follow other related articles on the PHP Chinese website!