Format Price with Currency Symbol in Magento 2 [the Right Way!]

It isn’t hard to format price values in Magento 2. There are many (wrong) ways to achieve the correct formatting of prices — or any value — in Magento 2. But fear not, today I will provide you with the right, clean way to format any value to a price (with currency symbol).

Although the entire professional Magento community is in agreement that you should never use the Object Manager. Because using the Object Manager’s bad. Many developers still advise using the Object Manager to create a Pricing Helper-Object. This is an absolute NO-NO!

So the next time someone at StackOverflow or anywhere advises you to use the Object Manager, you tell them:

Magento 2 offers something called Interfaces. Something that many developers appear to have missed. To format your values to a price the right way, you should use the PriceCurrencyInterface of the Magento Framework.

This interface contains a format()-method, to which you can just pass any value and it will return an appropriate formatted price. It has other options too.

  • The Wait is Over.

    Get the Newsletter you've always wanted, now!

    Sign up to receive Biweekly, Free Optimization Tips for WordPress.

    No spam. I promise.

  • How to Format Price using CurrencyInterface in Magento 2

    To implement this interface into your custom module through e.g. a Model, Controller, Plugin, or anything actually, you should use dependency injection:

    <?php
    namespace Daan\CustomModule\Model; // Or Controller, or Plugin, or anything actually.
    use Magento\Framework\Pricing\PriceCurrencyInterface as CurrencyInterface;
    class Custom {
    protected $currencyInterface;
    public function __construct(
    CurrencyInterface $currencyInterface
    ) {
    $this->currencyInterface = $currencyInterface;
    }
    // Let's imagine I passed a valid $productCollection here.
    public function getMyFormattedPrice( $productCollection ) {
    $prices = [];
    foreach ( $productCollection as $product ) {
    $prices[] = $this->currencyInterface->format(
    $product->getPrice(), // the price value.
    false, // don't include the container span.
    4 // default precision is two decimals, but I want 4 for some reason.
    );
    }
    return $prices;
    }
    }
    view raw Model\Custom.php hosted with ❤ by GitHub

    As you can see the format()-method allows three parameters:

    1. The value (obviously),
    2. A boolean (true/false) to define whether the container span should be included,
    3. An integer value to define the precision of the rendered value, which defaults to two.

    Formatting values to prices using the currency settings of the storeview is easy, because Magento Framework provides an interface to format price values. Most Magento 2 modules make their methods available through an interface, so you don’t have to use the Object Manager. In the end, I hope this will help you to create cleaner code in Magento 2. Any thoughts? Let me know!

    Daan van den Bergh

    Daan van den Bergh is a carefully seasoned web developer. His methods consist of thinly slicing your website’s beef and serve you with a platter of the best performance carpaccio on a bed of rocket — the only thing he’s more passionate about is food. Hire him at ffwp.dev.

    1 thought on “Format Price with Currency Symbol in Magento 2 [the Right Way!]”

    Leave a Comment

    This site uses Akismet to reduce spam. Learn how your comment data is processed.