Magento 2

How to Get Formatted Price With Currency In Magento 2

Magento 2

How to Get Formatted Price With Currency In Magento 2

Today we will understand the process to get formatted price with currency in Magento 2. Formatted price is required when you are delivering in places with different currencies. So, you need to get product price with format.

Using ObjectManager :

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceHelper = $objectManager->create('Magento\Framework\Pricing\PriceCurrencyInterface'); 
$price =  250; 
echo $priceHelper->convertAndFormat($price); //$250.00
echo $priceHelper->round($price); // 250 
echo $priceHelper->getCurrencySymbol(); //$
?>

Using Block File:

<?php
namespace Vendor\Module\Block;
class CustomPrice extends \Magento\Framework\View\Element\Template
{
    protected $currencyInterface; 
 
    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\Pricing\PriceCurrencyInterface $currencyInterface
     * @param array                                            $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Pricing\PriceCurrencyInterface $currencyInterface,
        array $data = []
    ) {
        $this->currencyInterface= $currencyInterface;
        parent::__construct($context, $data);
    }
 
    public function getCurrencyWithFormat($price)
    {
        return $this->currencyInterface->format($price,true,2);
    }
 
    public function getRoundedPrice($price)
    {
        return $this->currencyInterface->round($price);
    }
 
    public function getCurrentCurrencySymbol()
    {
        return $this->currencyInterface->getCurrencySymbol();
    }
}


I hope this blog proved to be helpful and easily understandable.