I have the following code that rounds my amounts to the nearest Dollar:
switch ($amazonResult['SalesRank']) {
case ($amazonResult['SalesRank'] < 1 || trim($amazonResult['SalesRank'])===''|| !isset($amazonResult['SalesRank']) || $amazonResult['SalesRank']=== null):
$Price=((float) $lowestAmazonPrice) * 0.05;
$payPrice = round($Price, 0); //to round the price up or down to the nearest $
break;
case ($amazonResult['SalesRank'] > 0 && $amazonResult['SalesRank'] <= 15000):
$Price=((float) $lowestAmazonPrice) * 0.20;
$payPrice = round($Price, 0); //to round the price up or down to the nearest $
break;
I understand that if I use round($Price, 2); that I will have 2 decimal places, but is there a way to round to the nearest 50 cents?
Multiply by 2, round to 0 in the digit above you want to round to .5 (round to the ones decimal place in your case), divide by 2.
That will give you rounding to the nearest .5, add on a 0 and you have rounding to the nearest .50.
If you want the nearest .25 do the same but multiply and divide by 4.