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?

2
  • You can use extension for it. It is simple extension. github.com/lillik/magento2-price-decimal Commented Dec 27, 2019 at 5: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. Commented Apr 11, 2023 at 4:04

9 Answers 9

0

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

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,
2
  • 1
    how to override it? Commented Mar 18, 2017 at 7: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 Commented Mar 31, 2021 at 9:18
2

You can do it using number_format funciton of php.

$price = 99.99;
number_format($price, 0, '.', '');
result : 99
1
  • what I need is , remove decimal points from magento using a module Commented Jan 9, 2016 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);
7
  • 1
    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 Commented Jan 9, 2016 at 8:57
  • Using Plugin you need to customize public method currency in following class Magento\Framework\Pricing\Helper\Data
    – Pratik
    Commented Jan 9, 2016 at 9:00
  • I checked that file but it seems it is not using PRECISION in currency method Commented Jan 9, 2016 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
    Commented Jan 9, 2016 at 9:03
  • ok I will check Commented Jan 9, 2016 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);
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!

0

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

Easy install and configuration. Manages front and backend.

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
) {
-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);
    }
2
  • 1
    the question was for Magento 2, not Magento 1
    – DarkMukke
    Commented Oct 21, 2016 at 11:28
  • and to change the core is not the best option I think. Plugin suggested.
    – vaso123
    Commented Mar 3, 2018 at 13:58

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