In the working environment, we will encounter such a scenario, that is, the submitted data, we need to determine whether the integer, decimal or string, so we initially use intval, if the string is directly converted to 0, does not meet the requirements, so we change the re to change. Without further ado, look at the code below

Public function index(){$accountPrice = '123.3'; // $accountPrice = 1112.; / / $accountPrice = 2324.1; / / $accountPrice = 2324.15; / / $accountPrice = 2324.157; //wrong // $accountPrice = 0.57; If (preg_match (' / ^ [0-9] + (. [0-9] {1, 2})? $/', $accountPrice)) { echo $accountPrice; }else{ echo 'NO'; }}Copy the code