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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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.
1 2 3 4 5 6 7 | //Your Price $price = 499; // Get Formatted Price $formattedPrice = $block->getFormattedPrice($price); echo $formattedPrice; |
2. Using Object Manager
1 2 3 4 5 6 7 8 9 10 11 | $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; |
Leave a Reply