tags:

views:

24

answers:

3

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 efficient way to do this... any suggestions? (using PHP).

+2  A: 
number_format($number, 2, '.', '')
GSto
I just looked at the man (http://php.net/manual/en/function.number-format.php) ... I assume the `, '')` at the end is to keep the commas in the thousands places from showing up?
aslum
yes, that's correct.
GSto
+1  A: 

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.

Marc B
+1  A: 
$number[$i] = round((float)$number[$i], 2);
thetaiko