Search code examples
phpzend-framework2currencybitcoin

How do I use ZF2 I18n currencyformatter to format bitcoin?


Bitcoin allows 8 decimal places (123.45678912).

How can I use ZF2 I18n currency formatter to format a bitcoin amount with the full 8 decimal places instead of just cutting it short to 2 decimal places?


Solution

  • If only I had spent a few more minutes doing a little more research:

    From ZF2's documentation at http://framework.zend.com/manual/2.3/en/modules/zend.i18n.view.helpers.html

    I found the following link:

    http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#details

    If you scroll down to the decimal format and significant digits sections, you'll see the answer. I ended up doing this for my code to represent BTC:

    $cur = getCurrency();
    
    $this->plugin("currencyformat")->setCurrencyCode($cur);
    
    if($cur == "BTC"):
        $this->plugin("currencyformat")->setCurrencyPattern('@############### ¤');
    endif;
    

    The '@' means display at least one signification digit. The 15 '#'s plus the '@' means the maximum amount of significant digits to show is 16. The '¤' will display the currency at the end of the number.

    For MySQL, the column I'm using to store these amounts is DECIMAL(16,8) so this works perfect for me.

    If you have better suggestions, please feel free to share.