Magento 1.8 CE – Rounding Product Prices

currencyrounding

I need to round the products prices with precision 2, but with the last decimal equal to 0 or 5.
Example:

16,50€
76,70€
5,05€

Inserted prices have precision 4.

16,5963 need to be -> 16,60
16,5388 need to be -> 16,55

How can I do it?

Best Answer

Magento way of generating a price with currency symbol:

$pow = pow(10, 2); 
$someprice = (ceil($pow * $someprice) + ceil($pow * $someprice - ceil($pow * $someprice))) / $pow;
$someprice = Mage::helper('core')->currency($someprice, true, false);

Note that you need to pass false as the third argument if you don't want to returned price to have a containing <span class="price"> element - both the second and third arguments are true by default.

Non Magento way to just format a float value for 2 decimal places:

$pow = pow(10, 2); 
$someprice = (ceil($pow * $someprice) + ceil($pow * $someprice - ceil($pow * $someprice))) / $pow;
$someprice = number_format($someprice, 2);
Related Topic