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

php verify whether it is two decimal places

angryTom
Release: 2023-02-28 10:52:02
Original
6228 people have browsed it

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

Result:

111错
1112.01对
2324.1错
2324.15对
2324.157错
0.57对
Copy after login

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!

Related labels:
php
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