Format Magento currency or number base on locale setting

How do we change the currency format in Magento (this article using Magento 1.7)? Magento doesn't allow custom currency format in admin panel. However, the currency format of Magento is base on Zend Framework currency locale format, so, what should we do is easy, just change locale setting of Zend Framework in Magento.

The Zend locale setting is in /lib/Zend/Locale/Data directory. There are many locale setting files. You could edit all setting files if you can or just edit the locale file that equivalent your website locale setting in System/Configuration/General/Locale Options/Locale

Magento locale options configuration

Example in the picture above, I have choose English (United States), so my Zend locale setting file is en_US.xml. The Zend locale setting file is base on the language and the territory (Ex: Vietnamese is vi_VN, English of United Kingdom is en_UK).

Now, just look at the en_US.xml file:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <ldml>
  3.         <identity>
  4.                 <version number="$Revision: 1.53 $" />
  5.                 <generation date="$Date: 2009/05/05 23:06:35 $" />
  6.                 <language type="en" />
  7.                 <territory type="US" />
  8.         </identity>
  9. </ldml>
Hide/show line number

It's not contain any information about currency format. Don't worry, because to be reduce the modification, Zend locale setting file inherit from the other, in the Data folder, we also see the en_UK.xml, en_GB.xml..., any locale file that begining with en_ will inherit from the en.xml setting file, if something isn't define in en.xml. Zend will read it from root.xml that is in Data folder too.

Look at the en.xml, we will see many default setting for English. If we want to custom for all territory that use English, we will edit in en.xml, if we want to custom for the special territory, we will edit the en_{territory}.xml file setting. This is some currency settings in en.xml file:

  1. ...
  2. <currency type="ADP">
  3.         <displayName>Andorran Peseta</displayName>
  4.         <displayName count="one">Andorran peseta</displayName>
  5.         <displayName count="other">Andorran pesetas</displayName>
  6. </currency>
  7. <currency type="AED">
  8.         <displayName>United Arab Emirates Dirham</displayName>
  9.         <displayName count="one">UAE dirham</displayName>
  10.         <displayName count="other">UAE dirhams</displayName>
  11. </currency>
  12. ...
Hide/show line number

And the currency format is

  1. ...
  2. <currencyFormats>
  3.         <currencyFormatLength>
  4.                 <currencyFormat>
  5.                         <pattern>¤#,##0.00;(¤#,##0.00)</pattern>
  6.                 </currencyFormat>
  7.         </currencyFormatLength>
  8.         <unitPattern count="one">{0} {1}</unitPattern>
  9.         <unitPattern count="other">{0} {1}</unitPattern>
  10. </currencyFormats>
  11. ...
Hide/show line number

We could change the currency format here and must be follow the rule

Token Description Example format Generated output
#0 Generates a number without precision and separation #0 1234567
, Generates a separation with the length from separation to next separation or to 0 #,##0 1,234,567
#,##,##0 Generates a standard separation of 3 and all following separations with 2 #,##,##0 12,34,567
. Generates a precision #0.# 1234567.1234
0 Generates a precision with a defined length #0.00 1234567.12


In en.xml file we don't see the setting of currency symbol, because it is already define in root.xml setting file and Zend don't want to define another value. If we want to define the special symbol for only English (United States) we can edit the en_US.xml as follow:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <ldml>
  3.         <identity>
  4.                 <version number="$Revision: 1.53 $"/>
  5.                 <generation date="$Date: 2009/05/05 23:06:35 $"/>
  6.                 <language type="en"/>
  7.                 <territory type="US"/>
  8.         </identity>
  9.         <numbers>
  10.                 <currencies>
  11.                         <currency type="USD">
  12.                                 <symbol>Dollar</symbol>
  13.                         </currency>
  14.                         <currency type="VND">
  15.                                 <symbol>Vietnamese dong</symbol>
  16.                         </currency>
  17.                 </currencies>
  18.         </numbers>
  19. </ldml>
Hide/show line number

Because of Zend Cache, so we won't see any change in the frontpage. We must clean the Zend Cache by delete all file and folder in /var/cache. Just refresh your frontpage and show the changing. The USD symbol become Dollar and the VND symbol become Vietnamese dong instead of đ.

If we change the currency symbol in root.xml, it will affect all locale setting unless the locale setting has its special setting.

Yeah, now, we could custom the currency or any number format setting in Magento. Just add or edit the Zend locale setting file and clear the Cache. Hope this help!