14

How to remove the price decimal from Front end price display only on the category and product pages ?

  • For the front end price display on the category and product pages? or are you asking about another part of the site? – circlesix Jun 25 '16 at 18:24
9

If you turn on the template hints for your site you will see the price is set in this template:

/vendor/magento/module-catalog/view/base/templates/product/price/amount/default.phtml

Copy this file into your theme

(app/design/frontend/{{namespace}}/{{theme_name}}/Magento_Catalog/templates/product/price/amount/default.phtml).

Take note of the class that this file is defined from

\Magento\Framework\Pricing\Render\Amount

The price itself is coming from this call:

<?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?>

If we take a look at the file that this method is created based on the class it's inheriting (/vendor/magento/framework/Pricing/Render/Amount.php) you will see this method:

/**
 * Format price value
 *
 * @param float $amount
 * @param bool $includeContainer
 * @param int $precision
 * @return float
 */
public function formatCurrency(
    $amount,
    $includeContainer = true,
    $precision = PriceCurrencyInterface::DEFAULT_PRECISION
) {
    return $this->priceCurrency->format($amount, $includeContainer, $precision);
}

Taking note of the perams in the method, we can see from the template that is displaying the price that we have the definition for the $amount and the $includeContainer but not for the last one $precision which defaults to the constant PriceCurrencyInterface::DEFAULT_PRECISION. This constant is defined in the file /vendor/magento/framework/Pricing/PriceCurrencyInterface.php. Looking in this file we see at the top of the file just after the interface is defined const DEFAULT_PRECISION = 2;. So in the templale we just have to redefine this constant and it will change the default precision based on this value. Setting the value to 0 will do the trick for the category page and for the product page.

Change:

<?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?>

to:

<?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer(), 0) ?>

Note

This works on the site that I tested on, using USDs. Because Magento supports international currencies and different translations, this method might not work exactly as it does for me. But I don't have any international site set up right now, so I'm not able to test.

| improve this answer | |
  • That is perfect working fine wherever there is a price except in product page – Ahmad Darwish Jun 26 '16 at 19:50
  • Yeah, i just saw that. The price get reloaded and changed with some JS in this file /vendor/magento/module-catalog/view/base/web/js/price-box.js. I just need to look into how to override this behavior. – circlesix Jun 26 '16 at 22:42
  • I found this while looking around, it's not an answer to the question, but might get you down the right path. magento.stackexchange.com/questions/112102/… – circlesix Jun 27 '16 at 0:08
  • I think this in combination with the JS solution is the right answer to the question. PHP handles rendering the prices from the server, whereas JS handles rendering the dynamically written prices (like in the minicart or when switching between options for configurable products). – Giel Berkers Mar 2 '17 at 8:32
  • 1
    this is awesome answer.. can you tell me how to remove decimal from product details page – Sarvesh Tiwari May 3 '18 at 11:22
6

The price-utils.js solution did not work for me.

But this worked in Magento 2.1: Look in /vendor/magento/framework/Pricing/PriceCurrencyInterface.php

Change const DEFAULT_PRECISION = 2;

To const DEFAULT_PRECISION = 0;

Then flush cache.

| improve this answer | |
  • Hi, I tried this answer and changed price in frontend only. how to change in backend also ? – Jsparo30 May 1 '17 at 12:41
  • its work for me but edit vendor file not good practices – matinict Jun 11 '18 at 6:15
5

To correct the product page you also should modify next javascript:

/vendor/magento/module-catalog/view/base/web/js/price-utils.js

on line 38 change

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

for

var precision = 0,

Hope this help.

I found this in: Magento 2 : How to remove decimal points from the price

| improve this answer | |
  • Don't modify core files, since they will be overwritten when you upgrade or re-install your installation. Rather use an override in your own design like where the article links to suggests. – Giel Berkers Mar 2 '17 at 8:31
1

You can use this module

https://github.com/lillik/magento2-price-decimal

if you are using composer, just run

composer require lillik/magento2-price-decimal
| improve this answer | |
  • This module caused several issues for us, product prices was corrupted – cjohansson Mar 1 '18 at 8:17
  • While you create new product it mess up all product prices, if a product price is 2,500 it will become 2 as it will remove all digits which come after comma (,) in price. – Naveed Asim May 26 '18 at 17:41
  • Its not working , product price getting corrupted totally.......... – Chala Chalapathi Sep 23 '19 at 11:20
0

Change this in the file price-utils.js

from

            (precision ? decimalSymbol + am.toFixed(2).replace(/-/, 0).slice(2) : '');

to

            (precision ? decimalSymbol + am.toFixed(format.requiredPrecision).replace(/-/, 0).slice(2) : '');

It will dynamically load price precision and you don't have to do anything in js file.Just change in php file and will reflect here :)

| improve this answer | |
0

I fixed method formatPrecision in vendor/magento/module-directory/Model/Currency.php on line ~301 there was code:

if (!isset($options['precision'])) {
    $options['precision'] = $precision;
}

I fixed so

if (!isset($options['precision'])) {
    $options['precision'] = ( (floatval($price) - intval($price)) != 0 ? $precision : 0);
}
| improve this answer | |
-1

To get desired result you have to edit this file

/pub/static/frontend/Theme-Package/Theme-Name/en_US/Magento_Catalog/js/price-utils.js

at line 38

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

replace this with

var precision = 0,

This is for magento2.1.3 this will remove decimals from price digits. This solution helped me

| improve this answer | |
  • Realize that static generated files (the files in pub/static) are overwritten every time you run setup:static-content:deploy. So I wouldn't make this modification here, but rather override /vendor/magento/module-catalog/view/base/web/js/price-utils.js, like Diego suggested. – Giel Berkers Mar 2 '17 at 8:30
  • don't modify pub/static as mentioned by Giel already. – dchayka Jul 27 '17 at 18:22

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