开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜