5

What is the best way to change the currency's precision?

I have a store that uses Euro as the currency, but I want to change the precision from 2 decimal places to 3.

I know that the locale data is stored under /lib/Zend/Locale/Data but is this really the best place to change this sort of information.

  • 1
    I would be concerned about the reliability of changing this. I believe that Magento (unfortunately) has some hardcoded assumptions regarding currency precision. Also, Zend_Locale_Data makes some assumptions about the relative location of the localization XML files :-/ – benmarks Jan 14 '14 at 13:56
  • Yeah the 2 is hard coded in place for Magento :( – David Manners Jan 14 '14 at 14:00
2

Ok first of all I saw the event currency_display_options_forming and I thought that this would be so easy, but sadly that was not the case :( You can use this event to change the display of the currency across the whole site but the follow code stops the precision from working in all cases.

Under Mage_Directory_Model_Currency there is a function formatPrecision. Here it checks for the options and if it does not find one for precision it will hard code it to 2!

So the first thing I did as Marius said is to rewrite the Mage_Directory_Model_Currency::format I chose to set it up with xml (great minds think alike)

public function format($price, $options = array(), $includeContainer = true, $addBrackets = false)
{
    return $this->formatPrecision(
        $price,
        Mage::getStoreConfig(self::XML_PATH_CURRENCY_ALLOW), // Mage uses '2' here
        $options,
        $includeContainer,
        $addBrackets
    );
}

But that only changes the display. Most of the price displays all call Mage_Core_Model_Store::roundPrice and this again has a hard coded 2, I am starting to think this was a bad idea.

public function roundPrice($price)
{
    return round($price, 2);
}

So the obvious thing now is to rewrite the roundPrice function and again use the same xml config value.

public function roundPrice($fPrice)
{
    return round($fPrice, Mage::getStoreConfig(Sitewards_Currency_Model_Format::XML_PATH_CURRENCY_ALLOW));
}

Now that covers most of the front-end if you also want to change this in the admin section that would involve changing templates and rewriting at least Mage_Adminhtml_Block_Catalog_Product_Helper_Form_Price

public function getEscapedValue($iIndex=null)
{
    $fValue = $this->getValue();

    if (!is_numeric($fValue)) {
        return null;
    }

    return number_format($fValue, Mage::getStoreConfig(Sitewards_Currency_Model_Format::XML_PATH_CURRENCY_ALLOW), null, '');
}

One not on the template front, the one I know about now is catalog/product/edit/price/tier.phtml as again that has <?php echo sprintf('%.2f', $_item['price']) ?>

On one final note the admin grid actually does work with the first event currency_display_options_forming.

I will update this answer the more I find out, but it is still a work in progress.

1

I think you can override the method Mage_Directory_Model_Currency::format.
Right now it looks like this:

 public function format($price, $options=array(), $includeContainer = true, $addBrackets = false)
{
    return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
}

The second parameter for formatPrecision is the number of decimals. Change it to :

 public function format($price, $options=array(), $includeContainer = true, $addBrackets = false)
{
    return $this->formatPrecision($price, 3, $options, $includeContainer, $addBrackets);
}

Or if you want to be "politically correct", to this:

 public function format($price, $options=array(), $includeContainer = true, $addBrackets = false)
{
    return $this->formatPrecision($price, Mage::getStoreConfig('some/path/here'), $options, $includeContainer, $addBrackets);
}

and define a config setting that you can change later.

  • Oh Marius I wish it was that easy ;) that is about a third of what I have done so far. Will post the complete (as in how far we have tested) answer soon. – David Manners Jan 18 '14 at 8:31
  • 1
    @DavidManners I imagined it was not only this, but I thought that a pattial answer is better than no answer. – Marius Jan 18 '14 at 8:34
  • Yup you are right. I am typing up my slightly less partial answer now also :) – David Manners Jan 18 '14 at 8:41
  • Please I am new to Magento 2, I need the full solution files. – Jsparo30 May 4 '17 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.