Rounding a number up or down is important because it helps to make sure that we are precise with our numbers. When we round a number, we are making sure that it is accurate and easier to work with.
For example, when you are trying to calculate the total cost of an item at the grocery store, if you were to use the exact price, it would be more difficult to calculate in your head. However, if you were to round the price up or down to the nearest dollar, it would be much easier to calculate.
The ceil() and floor() functions are rounding functions in PHP. The ceil() function rounds a number up to the nearest integer, while the floor() function rounds a number down to the nearest integer.
The round() function is another rounding function in PHP. This function rounds a number to the nearest integer, using the decimal places that are specified.
Round to the nearest 5:
$roundedNumber = (ceil($number)%10 === 0) ? ceil($number) : ceil($number/5)*5; $roundedNumber = (floor($number)%10 === 0) ? floor($number) : floor($number/5)*5;
Round to the nearest 10:
$roundedNumber = round($number, -1, PHP_ROUND_HALF_UP); $roundedNumber = round($number, -1, PHP_ROUND_HALF_DOWN); $roundedNumber = ceil($number / 10) * 10; $roundedNumber = floor($number / 10) * 10;