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?

up vote 26 down vote accepted

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

$this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format(50,2),true,false);
  • What if you just want currency symbol to the price? currency() method will used current scope currency to convert. – MagePsycho Feb 13 at 11:24
  • 1
    This does not work for prices over 1000. The number_format adds thousands separators, which causes trouble on European systems where a point is a thousand separator. For example a price of EUR 12000 returns € 12,00 which is wrong. Then you need to remove the number_format() and just pass the amount: $this->helper('Magento\Framework\Pricing\Helper\Data')->currency(50),true,false); – Jacco Amersfoort Mar 22 at 14:12
  • This is certainly not the best answer althoug it is voted highest and the accepted one. Better approuch is dchayka's answer: magento.stackexchange.com/a/148082/5827 – Akif May 16 at 13:35
  • For those wondering, the signature is currency($value, $format = true, $includeContainer = true) – Collin Anderson Jun 20 at 14:27

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 Magento\Directory\Model\Currency. 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.

  • Another benefit of using this method is working with values over $1000. The other answers don't take this number format in mind, and you end up with the incorrect prices showing up on the front end. I wish i could give you 1,000 up votes – circlesix Apr 17 '17 at 20:32
  • 1
    @circlesix thanks a lot, much appreciated. – dchayka Apr 18 '17 at 2:34
  • So what does Magento\Directory\Model\Currency do, and why should it be used? Does it look at the current storeview currency configuration, or the currency selected by the customer? – Erfan Aug 31 '17 at 3:57
  • 1
    @Erfan the way I see it Currency model in the Directory module is like a helper to manipulate final price value. In the example of the helper from the Framework you'll be able to convert the price to the correct currency of the store but you have no control over precision of the amount and will have to extend classes to get it customized. The Currency is the opposite where it expects the final amount but allows you to customize formatting, decimal precision, etc. – dchayka Jul 25 at 16:48

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

$price = 5.5;
$this->helper('Magento\Framework\Pricing\Helper\Data')->currency($price,true,false);
  • For those wondering, the signature is currency($value, $format = true, $includeContainer = true) – Collin Anderson Jun 20 at 14:31
  • Check link bit.ly/2MKg1L4 – Rakesh Jesadiya Aug 23 at 17:30
  • This is the quickest way, if you don't want to build a separate module, just to implement the currencyInterface into Magento's Template-class. – Daan van den Bergh Oct 2 at 11:43

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 Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\View\Element\Template;

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

    public function __construct(
        Template\Context $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!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.