开发者

Easy way to check that something is a number w/ max 2 decimal places of accuracy?

开发者 https://www.devze.com 2022-12-25 12:58 出处:网络
I want to make sure the input is A)a number and B)has at most 2 decimals. $number[$i]=int(100*$number[$i])/100;

I want to make sure the input is A)a number and B)has at most 2 decimals. $number[$i]=int(100*$number[$i])/100;

I imagine there is a more effi开发者_运维问答cient way to do this... any suggestions? (using PHP).


number_format($number, 2, '.', '')


Regexes to the rescue:

if (preg_match('/^\d+\.\d{2}$/', $number[$i])) {
   etc...
}

of course, now that regexes are involved, you've got two problems, as the old saying goes.


$number[$i] = round((float)$number[$i], 2);
0

精彩评论

暂无评论...
验证码 换一张
取 消