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;
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);
精彩评论