2

I need to remove the two decimals from product prices on the frontend. For example, if the price is $49.00 it should be displayed as $49.

Since my products have no floating points it is ok to remove decimals in my case. On the product page, it seems like the price is loaded using JavaScript, so it is hard to do it by changing price/amount/default.phtml. Can someone suggest a proper method?

danbtl
  • 5
  • 2
Chamal Chamikara
  • 1,586
  • 4
  • 21
  • 32
  • You can use extension for it. It is simple extension. https://github.com/lillik/magento2-price-decimal – Chirag Parmar Dec 27 '19 at 05:09
  • Just a word of warning with using 'github.com/lillik/magento2-price-decimal' as it has issues where it strips the thousand operator making $1,299 equal $1. Had issues using this module. – MetalMonkey Apr 11 '23 at 04:04

9 Answers9

4

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,
7ochem
  • 7,532
  • 14
  • 51
  • 80
Xubaer
  • 41
  • 3
  • 1
    how to override it? – Ahmad Darwish Mar 18 '17 at 07:54
  • Therefore you would have to add a requirejs-config.js file to some module (/app/code/VENDOR/MODULE/view/frontend/requirejs-config.js) with content var config = {
    map: {
    
        '*': {
    
            "Magento_Catalog/js/price-utils": "VENDOR_MODULE/js/price-utils"
    
        }
    
    }
    
    

    }; Then create a file price-utils.js like /app/code/VENDOR/MODULE/view/frontend/web/js/price-utils.js. For German explanation please see here

    – Bastian Kretzschmar Mar 31 '21 at 09:18
2

You can do it using number_format funciton of php.

$price = 99.99;
number_format($price, 0, '.', '');
result : 99
Rakesh Jesadiya
  • 42,221
  • 18
  • 132
  • 183
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);
open-ecommerce.org
  • 591
  • 1
  • 5
  • 24
0

Follow the below blog post for changing the Price formatting. You will require to override the format class of Magento Magento\Framework\Locale\Format your custom module. And then override the function getPriceFormat in format class.

https://webkul.com/blog/change-format-price-magento-2/

In the code, by changing requiredPrecision => 0, it will set values without decimals.

Hope this helps!

Bhaumik Upadhyay
  • 893
  • 1
  • 9
  • 18
0

Here is the module. You can review or Download from here.

Vrajesh Patel
  • 1,964
  • 2
  • 12
  • 28
0

We are using this free Open Source module: https://github.com/lillik/magento2-price-decimal.

Easy install and configuration. Manages front and backend.

DavyC
  • 11
  • 4
0

For frontend in Adobe Commerce 2.4.4

Create a before Plugin, with

$precision = 0

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

formatPrecision(

public function formatPrecision(
    $price,
    $precision,
    $options = [],
    $includeContainer = true,
    $addBrackets = false
) {
brjupo
  • 75
  • 1
  • 4
0

You can do using price helper class

$this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format(50,2),true,false);
Pratik
  • 3,805
  • 6
  • 31
  • 49
-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);
    }
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
user40157
  • 1
  • 1