What potential pitfalls should be considered when using floor() for rounding calculations in PHP?
When using the floor() function for rounding calculations in PHP, one potential pitfall to consider is that it always rounds down to the nearest integer. This means that if you are working with negative numbers, the result may not be what you expect. To address this issue, you can use a combination of floor() and ceil() functions to ensure proper rounding for both positive and negative numbers.
function customFloor($number) {
if ($number >= 0) {
return floor($number);
} else {
return ceil($number);
}
}
// Example usage
$number = -3.14;
$roundedNumber = customFloor($number);
echo $roundedNumber; // Output: -4