Tuesday, October 21, 2008

Rounding a Floating Point Number

// define floating point number
$num = (2/3);

// round to integer
// result: 1
$roundNum = round($num);
echo $roundNum . "\n";

// round to 1 decimal place
// result: 0.7
$roundNum = round($num, 1);
echo $roundNum . "\n";

// round to 3 decimal places
// result: 0.667
$roundNum = round($num, 3);
echo $roundNum;

// define floating point numbers
$num = (1/3);
$r = round($num);
$c = ceil($num);
$f = floor($num);
// result: "0 1 0"
echo "$r $c $f"

No comments: