How do I truncate a decimal in PHP?



PHP Snippet 1:

intval(1234.567);
intval(-1234.567);

PHP Snippet 2:

echo bcdiv(2.56789, 1, 1);  // 2.5
echo bcdiv(2.56789, 1, 3);  // 2.567
echo bcdiv(-2.56789, 1, 1); // -2.5
echo bcdiv(-2.56789, 1, 3); // -2.567

PHP Snippet 3:

(int) 1234.567; //  1234
(int) -1234.567; // -1234

PHP Snippet 4:

if($number < 0 )
$res = round($number);
else
$res = floor($number); 

echo $res;

PHP Snippet 5:

function truncate($number, $precision = 0) {
   // warning: precision is limited by the size of the int type
   $shift = pow(10, $precision);
   return intval($number * $shift)/$shift;
}