Thursday, December 24, 2009

Currency Rounding (for jjones17)

Problem:
Round the currency to the nearest ten ore (ten cents)

Possible solution:

Here is what you can try.
In lib/Zend/Currency.php, in the $_options member of the class change precision from 2 to 1.
This will have the desired effect but also side effects.
All the other currencies besides Norwegian Krone they will all be rounded in the same way.
All prices will look like this 123.4 instead of 123.40.
In order to avoid the first problem instead of changing the precision you can do a "quick and dirty" job.
in Mage_Directory_Model_Currency class for method formatTxt() add this right at the beginning.
if ($this->getCode() == 'NOK'){//if you need it for an other //currency just replace the code here
$options['precision'] = 1;
}

If you want to avoid the last problem (123.4 instead of 123.40) in the same method formatTxt() add add a 0 to the return value
return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options)."0";

If you don't mind these 2 side effects than I recommended using the first solution. It's less code.

No comments:

Post a Comment