2

I've displayed item's price (both old price and new price) using this code,

<?php echo $_item->getProduct()->getPrice(); ?>  

But when I see the page, it displays price with 4 decimal numbers, like 150.0000

I need to change number of decimals after the . to be two. like 15.00

How can I do this? I'm okay with however getting formatted number, when $_item->getProduct()->getPrice(); gives 15.0000 I need to change it into 15.00 however way possible.

Can anybody help me?

- NOTE -

I've found php way to format the numbers.

<?php $oldprice = $_item->getProduct()->getPrice();
             echo number_format($oldprice, 2, ".", ","); 
             ?> 

But the problem is, I've to put this everywhere I need to change the format

3 Answers 3

8

You can format the Price using the below Code:

public function __construct(
    /* Add below dependencies */
    \Magento\Framework\Pricing\Helper\Data $pricingHelper,
) {
    $this->pricingHelper = $pricingHelper;
}

/*Call the below function to format amount*/
$this->pricingHelper->currency($amount,true,false);

In template file, you can use the below code:

$pricHelper = $this->helper('\Magento\Framework\Pricing\Helper\Data');
echo $pricHelper->currency($amount,true,false);
3
  • Hi, where should I put this?
    – Joey
    Commented Nov 8, 2017 at 11:35
  • I have updated the answer, in template you can use like that way Commented Nov 8, 2017 at 11:41
  • sorry, im not an expert. where should I put the construct?
    – Joey
    Commented Nov 9, 2017 at 8:44
6

You can use this format

Using Object manager :

$price=$objectManager->create('Magento\Framework\Pricing\Helper\Data')->currency(number_format($value->getPrice(),2));

Using Construct :

public function __construct(
    /* Add below dependencies */
    \Magento\Framework\Pricing\Helper\Data $pricingHelper,
) {
    $this->pricingHelper = $pricingHelper;
}

/*Call the below function to format amount*/
$this->pricingHelper->currency($price,true,false);

Note : Do not use object manager directly.

6
  • 1
    to where should I need to put this? I need to apply this filter throughout my theme. to whole site. To every place that display price
    – Joey
    Commented Nov 9, 2017 at 8:45
  • Where you display price, You can set this code instead of your code. Commented Nov 9, 2017 at 9:05
  • 1
    If it is useful and right answer , Please approve this answer. So, This code is useful to other people. :) Commented Nov 9, 2017 at 9:06
  • I need to set this filter to my whole site, without editing core files, but only inside theme folder. can I do that? if yes, how?
    – Joey
    Commented Nov 9, 2017 at 9:49
  • I agian told you that from where you display your product price at that place you can set this code instead of your code. If you display your product price from theme then put there. Otherwise, you need to change in all files from where you display. Commented Nov 9, 2017 at 10:11
2

For change Currency formate please change in below files

vendor/magento/framework/Pricing/PriceCurrencyInterface.php

const DEFAULT_PRECISION = 2;

vendor/magento/module-directory/Model/Currency.php

return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);

vendor/magento/module-sales/Model/Order.php

public function formatPrice($price, $addBrackets = false) { return $this->formatPricePrecision($price, 2, $addBrackets); }

2
  • Appreciate the help. Isn't it bad practice to modify core files? either way, I need to acomplish the task without editing core files. I can edit items inside my theme so that's what I'm looking for. Can you help me with that?
    – Joey
    Commented Nov 9, 2017 at 8:43
  • You can use plugin to do this. Plugin is the best way to change the core behaviour. Commented Nov 12, 2017 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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