In this post I am going to explain how to get formatted price with currency in Magento 2.
1. Using Dependency Injection
Add below code snippet in Block class.
protected $_priceHelper;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Pricing\Helper\Data $priceHelper,
array $data = []
)
{
$this->_priceHelper = $priceHelper;
parent::__construct($context, $data);
}
public function getFormattedPrice($price)
{
$formattedPrice = $this->_priceHelper->currency($price, true, false);
return $formattedPrice;
}Add below code snippet in template file.
//Your Price $price = 499; // Get Formatted Price $formattedPrice = $block->getFormattedPrice($price); echo $formattedPrice;
2. Using Object Manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
//Your Price
$price = 499;
// Get Formatted Price
$formattedPrice = $priceHelper->currency($price, true, false);
echo $formattedPrice;
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!


Leave a Reply