How to get a formatted price with currency in phtml file in Magento 2
How to get a formatted price with currency in phtml file in Magento 2

How to get a formatted price with currency in phtml file in Magento 2

In this article, I will show you how you can get the formatted price with currency.

You can get the formatted price with class Magento\Directory\Model or Magento\Framework\Pricing\PriceCurrencyInterface or Magento\Framework\Pricing\Helper\Data

1. By using class Magento\Framework\Pricing\Helper\Data

The best way of doing this by using class Magento\Framework\Pricing\Helper\Data as a dependency.

This class has two methods currency and currencyByStore. You can use currency method to get the price with the current store currency. And to get the price with another store use currencyByStore method

Method explanation:

currency($value, $format = true, $includeContainer = true)

Here is the description of parameters of the currency method:

  1. $value(float) : Pass price value
  2. $format(bool ) : Formate value
  3. $includeContainer (bool): Use price HTML or not
currencyByStore($value, $store = null, $format = true, $includeContainer = true)

Here is the description of parameters of the currencyByStore method:

  1. $value(float) : Pass price value
  2. $store(int or \Magento\Store\Model\Store) : Pass store Id or store model
  3. $format(bool ) : Format value
  4. $includeContainer (bool): Use price HTML or not

Get a formatted price in phtml file with $helper variable

$this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format(50,2),true,false);

With ObjectManager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
$price =  100;
$formattedPrice = $priceHelper->currency($price, true, false);

NOTE: ObjectManage is not a recommended way. Read more  ObjectManager – Magento 2 Developer Documentation

With dependency injection:

<?php
namespace Namespace\MyModule\Model;

class MyClass
{
    /**
     * @var Magento\Framework\Pricing\Helper\Data
     */
    private $pricingHelper;

    /**
     * MyClass constructor.
     * @param Magento\Framework\Pricing\Helper\Data $pricingHelper
     */
    public function __construct(\Magento\Framework\Pricing\Helper\Data $pricingHelper)
    {
        $this->pricingHelper = $pricingHelper;
    }

    /**
     * Convert and format price value for current application store
     *
     * @return  float|string
     */
    public function getFormattedPrice()
    {
        $price = 100;
        return $this->pricingHelper->currency($price, true, false);
    }

    /**
     * Convert and format price value for specified store
     *
     * @return  float|string
     */
    public function getFormattedPriceByStore()
    {
        $price = 100;
        $storeId = 1;
        return $this->pricingHelper->currencyByStore($price, $storeId, true, false)
    }
}

2. By using class Magento\Framework\Pricing\PriceCurrencyInterface

This interface has main three methods:

  1. convertAndFormat($price) : Convert simple price value to formatted price
  2. round($price) : To round price
  3. getCurrencySymbol() : To get current store currency

Here is the code snippet of how you can get the formatted price with the object manager and dependency injection.

With Object Manager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceInterface = $objectManager->create('Magento\Framework\Pricing\PriceCurrencyInterface');
$price =  70.0000; //Your Price
$priceInterface->convertAndFormat($price); // Output: $70.00
$priceInterface->round($price); // Output: 70
$priceInterface->getCurrencySymbol(); // Output: $

With Dependency Injection:

<?php
namespace Namespace\MyModule\Model;

class MyClass
{
    /**
     * @var \Magento\Framework\Pricing\PriceCurrencyInterface 
     */
    protected $priceCurrency;
    
    /**
     * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
     */
    public function __construct(
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
    ) {
        $this->priceCurrency = $priceCurrency;
    }

    /**
     * FGet current store currency symbol with price
     *
     * @param float $amount
     * @param bool $includeContainer
     * @param int $precision
     * @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
     * @param \Magento\Framework\Model\AbstractModel|string|null $currency
     * @return float
     */
    public function getCurrencyFormat($price, $includeContainer = false, $precision)
    {
        //Example: $this->priceCurrency->format($price,true,2);
        return $this->priceCurrency->format(
            $price,
            $includeContainer = true,
            $precision = self::DEFAULT_PRECISION,
            $scope = null,
            $currency = null
        );
    }

    /**
     * Round price
     *
     * @deprecated 102.0.1
     * @param float $price
     * @return float
     */
    public function getRoundPrice($price)
    {
        return $this->priceCurrency->round($price);
    }

    /**
     * Get CurrencySymbol of current store
     * @return string
     */
    public function getCurrencySymbol()
    {
        return $this->priceCurrency->getCurrencySymbol();
    }
}

Here is the output of above code snippet:

echo $this->getCurrencyFormat(70.0000); //output $70.00
echo $this->getRoundPrice(70.0000); //output 70 (without currency symbol)
echo $this->getCurrencySymbol(); //output $

I hope this will help you to understand how to get a formatted price in Magento 2. If you have any query feel free to comment below.

Please share this post and Follow me on social media via the following links to get updated with the latest post of Magento. Happy Coding…!