Magento 2: How to remove decimal points from the price
Question
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?
Solution
Here is the module. You can review or Download from here.
OTHER TIPS
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,
You can do it using number_format funciton of php.
$price = 99.99;
number_format($price, 0, '.', '');
result : 99
You can do using price helper class
$this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format(50,2),true,false);
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);
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.
In the code, by changing requiredPrecision => 0
, it will set values without decimals.
Hope this helps!
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);
}