How to convert number into currency format in magento2

The question:

I want to convert number into currency format in magento2 like we do in Magento 1.x with this

$_coreHelper = $this->helper('core');
$_coreHelper->currency(number_format(50,2),true,false)

How to do same in Magento2?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

In magento 2, there are no ‘core’ module. You can get this by following way inside view file(.phtml)

$this->helper('MagentoFrameworkPricingHelperData')->currency(number_format(50,2),true,false);

Method 2

What you want to do is to inject the “PriceCurrencyInterface” in the Block of the template file that you wish to use this in.

template.phtml

<div><?= $block->getFormatedPrice('342.4345') ?>

Item.php (Block Class of the above template… whatever that might be)

<?php
namespace Whatever

use MagentoFrameworkPricingPriceCurrencyInterface;
use MagentoFrameworkViewElementTemplate;

class Item extends Template
{
    /** @var PriceCurrencyInterface $priceCurrency */
    protected $priceCurrency;

    public function __construct(
        TemplateContext $context,
        PriceCurrencyInterface $priceCurrency,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->priceCurrency = $priceCurrency;
    }

    /**
     * Function getFormatedPrice
     *
     * @param float $price
     *
     * @return string
     */
    public function getFormatedPrice($amount)
    {
        return $this->priceCurrency->convertAndFormat($amount);
    }

This has the added benefit of displaying the correct format based on current store locale. It also offers other methods that might be helpful, check them out…

Make sure to check the method signature since you can configure the result you want to display such as the container and the precision.

priceCurrency->convertAndFormat($amount, $includeContainer, $precision)

Cheers!

Method 3

First of all do not do currency formatting inside your view (.phtml) files, use helpers or blocks or combination of both.

The accepted answer uses number_format function which should not be used at all, at least I wouldn’t go with that approach.

You can try using a model:

Model of type MagentoDirectoryModelCurrency.
Function format() as it’s the one responsible for decimal places and formatting.

Example assuming variables $model and $product have been instantiated:

$model->format($product->getPrice(), array('symbol' => ''), false, false)

2 decimal places for formatting without $ dollar next to the amount. Pass empty array() if you want store currency appended to your amount.

Method 4

You can get this by following way inside view file(.phtml).

$price = 5.5;
$this->helper('MagentoFrameworkPricingHelperData')->currency($price,true,false);

Method 5

Using the pricing helper

<?php
namespace VendorModule

class MyClass extends Template
{
    public $priceHelper;

    public function __construct(
        TemplateContext $context,
        MagentoFrameworkPricingHelperData $priceHelper,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->priceHelper  = $priceHelper;
    }

    public function myCustomFunction($product) {
        return $this->priceHelper->currency($product->getPrice(),true,false);
    }
}

This outputs a price and currency symbol from a given product.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment