Home  >  Article  >  Backend Development  >  php verify whether it is two decimal places

php verify whether it is two decimal places

angryTom
angryTomOriginal
2019-11-04 10:03:136138browse

php verify whether it is two decimal places

php verifies whether it is two decimal places

1. First create a function fun and receive a parameter num;

2. Then use preg_match within the function to perform regular matching on num;

3. If the final match is successful, num will be two decimal places.

The code is as follows:

<?php

function fun($num) {
	if (preg_match(&#39;/^[0-9]+(.[0-9]{2})$/&#39;, $num)) {
		echo $num, &#39;对<br>&#39;;
	}else{
	    echo $num, &#39;错<br>&#39;;
	}
}

fun(111);
fun(1112.01);
fun(2324.1);
fun(2324.15);
fun(2324.157);
fun(0.57);

Result:

111错
1112.01对
2324.1错
2324.15对
2324.157错
0.57对

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of php verify whether it is two decimal places. 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