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

青灯夜游
Release: 2023-03-13 12:58:02
Original
4992 people have browsed it

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);
?>
Copy after login

Output results:

5
Copy after login

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);
?>
Copy after login

Output result:

3
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template