General

How To Get Formatted Price With Currency In Magento 2

Hello Magento Folks?,

How are you all working? I hope all are safe and healthy. Welcome to Magento Tutorial blog where we will learn How To Get Formatted Price With Currency In Magento 2. Also, check our latest and interesting article published where I have explained Why 75% of Your Marketing Emails are Never Opened and What to Do About It. Let’s Dive In?.

Introduction:

Magento 2 provides many methods to implement the correct formatting of prices or any type of value. In Magento 2 you can achieve formatted price with currency by using the object manager. One can easily convert a simple number into currency by implementing any of the given below solutions.

How to Get Formatted Price With Currency ? In Magento 2:

Method 1: Using the Block file

Create one block file on our custom extension add Customprice.php in the following path

app\code\Vendor\Extension\Block\Customprice

<?php

namespace Vendor\Extension\Block;


class Customprice extends \Magento\Framework\View\Element\Template
{

    protected $priceCurrency; 

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        array $data = []
    ) {
        $this->priceCurrency = $priceCurrency;
        parent::__construct($context, $data);
    }

    public function getCurrencyWithFormat($price)
    {
        return $this->priceCurrency->format($price,true,2);
    }

    public function getRoundedPrice($price)
    {
        return $this->priceCurrency->round($price);
    }

    public function getCurrentCurrencySymbol()
    {
        return $this->priceCurrency->getCurrencySymbol();
    }
}

Method 2: Using ObjectManager

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

Note: Using ObjectManager for Magento development is not recommended by us and Magento. This is just for Knowledge purposes.

Over to You:

Hence, you can choose any of the above methods to implement in your store if you require to format currency in Magento 2. In case any casualties faced raising the queries in the comment section below. If the article is helpful then share it with your Developer friend. 

Happy Reading? 

Click to rate this post!
[Total: 8 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago