PHP tutorial: Round to the nearest nickel, dime or quarter
Here are examples on how to round up to the nearest nickel, dime, or quarter using PHP. I will also show you how to round down to the nearest nickel, dime or quarter in PHP. In PHP, the round function will always round up. The floor function will always round down. Because we are talking about money here, I will be using the PHP function number_format to make sure that all the zeros are in place.
Here is how to round up or down to the nearest nickel (5 cents):
$round_up_to_nearest_nickel=number_format(round($number*20)/20,2);
$round_down_to_nearest_nickel=number_format(floor($number*20)/20,2);
Here is how to round up or down to the nearest dime (10 cents):
$round_up_to_nearest_dime=number_format(round($number*10)/10,2);
$round_down_to_nearest_dime=number_format(floor($number*10)/10,2);
Here is how to round up or down to the nearest quarter (25 cents):
$round_up_to_nearest_quarter=number_format(round($number*4)/4,2);
$round_down_to_nearest_quarter=number_format(floor($number*4)/4,2);