TransWikia.com

Magento 2: How to remove decimal points from the price

Magento Asked by Chamal Chamikara on December 14, 2021

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?

8 Answers

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

Easy install and configuration. Manages front and backend.

Answered by Capiau on December 14, 2021

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

Answered by Vrajesh Patel on December 14, 2021

Follow the below blog post for changing the Price formatting. You will require to override the format class of Magento MagentoFrameworkLocaleFormat 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!

Answered by Bhaumik Upadhyay on December 14, 2021

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

$objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of Object Manager
$currencyModel = $objectManager->create('MagentoDirectoryModelCurrency'); // 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);

Answered by open-ecommerce.org on December 14, 2021

You can do using

appcodecoreMageDirectoryModelCurrency.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);
    }

Answered by user40157 on December 14, 2021

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,

Answered by Xubaer on December 14, 2021

You can do using price helper class

$this->helper('MagentoFrameworkPricingHelperData')->currency(number_format(50,2),true,false);

Answered by Pratik on December 14, 2021

You can do it using number_format funciton of php.

$price = 99.99;
number_format($price, 0, '.', '');
result : 99

Answered by Rakesh Jesadiya on December 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP