Home  >  Article  >  Backend Development  >  How to determine how many decimals there are in php

How to determine how many decimals there are in php

青灯夜游
青灯夜游Original
2021-10-20 19:16:484936browse

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.

How to determine how many decimals there are in php

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, &#39;.&#39;);
         $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(&#39;.&#39;,$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!

Statement:
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