1

I need to remove two decimal points from product prices on the frontend. For example if the price is $49.00 it should be $49.Since my products has no floating points it is ok to remove decimal points in my case. On the product page, it seems price is loading from a javascript so it is hard to do it by changing price/amount/default.phtml. Can someone suggest a proper method?

3

You need to override vendor/magento/module-catalog/view/base/web/js/price-utils.js and change the value of precision on line 38:

from

var precision = isNaN(format.requiredPrecision = Math.abs(format.requiredPrecision)) ? 2 : format.requiredPrecision,

to

var precision = 0,
| improve this answer | |
1

You can do it using number_format funciton of php.

$price = 99.99;
number_format($price, 0, '.', '');
result : 99
| improve this answer | |
  • what I need is , remove decimal points from magento using a module – Chamal Chamikara Jan 9 '16 at 8:58
0

You can do using price helper class

$this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format(50,2),true,false);
| improve this answer | |
  • I may able to change using this approach in one place but what I need is to change it globally which mean it should appear anywhere we use price block – Chamal Chamikara Jan 9 '16 at 8:57
  • Using Plugin you need to customize public method currency in following class Magento\Framework\Pricing\Helper\Data – Pratik Jan 9 '16 at 9:00
  • I checked that file but it seems it is not using PRECISION in currency method – Chamal Chamikara Jan 9 '16 at 9:02
  • You need to customization using after plugin method. Also if you have knowledge regarding interface check PriceCurrencyInterface so you get some idea. – Pratik Jan 9 '16 at 9:03
  • ok I will check – Chamal Chamikara Jan 9 '16 at 11:58
0

Just get the price as a normal attribute and then apply the currencyModel

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
$currencyModel = $objectManager->create('Magento\Directory\Model\Currency'); // Instance of Currency Model
//get currency symbol by currency code
$currencyCode = 'GBP';
$currencySymbol = $currencyModel->load($currencyCode)->getCurrencySymbol();
$precision = 0;   // for displaying price decimals 2 point

...

$annualPrice = $_product->getResource()->getAttribute('price')->getFrontend()->getValue($_product);

$annualPrice = $currencyModel->format($annualPrice, ['symbol' => $currencySymbol, 'precision'=> $precision], false, false);
| improve this answer | |
-2

You can do using

app\code\core\Mage\Directory\Model\Currency.php

you find the following code :

public function formatTxt($price, $options = array()) {
        if (!is_numeric($price)) {
                $price = Mage::app()->getLocale()->getNumber($price);
        }
        /**
         * Fix problem with 12 000 000, 1 200 000
         *
         * %f - the argument is treated as a float, and presented as a floating-point number (locale aware).
         * %F - the argument is treated as a float, and presented as a floating-point number (non-locale aware).
         */
        $price = sprintf("%F", $price);
        if ($price == -0) {
                $price = 0;
        }

        return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options);
    }

and change with the following code :

public function formatTxt($price, $options = array()) {
        if (!is_numeric($price)) {
                $price = Mage::app()->getLocale()->getNumber($price);
        }
        /**
         * Fix problem with 12 000 000, 1 200 000
         *
         * %f - the argument is treated as a float, and presented as a floating-point number (locale aware).
         * %F - the argument is treated as a float, and presented as a floating-point number (non-locale aware).
         */
        $price = sprintf("%F", $price);
        if ($price == -0) {
                $price = 0;
        }


        $options["precision"] = 0;
        if (isset($options["precision"])) {
                $price = round($price, $options["precision"]);
            }


        return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options);
    }
| improve this answer | |
  • 1
    the question was for Magento 2, not Magento 1 – DarkMukke Oct 21 '16 at 11:28
  • and to change the core is not the best option I think. Plugin suggested. – vaso123 Mar 3 '18 at 13:58

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