How to change currency format in Magento 2?

create just simple module and overrider default *Format.php** file,

app/code/Package/Modulename/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\Locale\Format" type="Vendor\Currency\Model\Format" />
</config>

create model file, app/code/Package/Modulename/Model/Format.php

<?php
namespace Package\Modulename\Model;

use Magento\Framework\Locale\Bundle\DataBundle;

class Format extends \Magento\Framework\Locale\Format
{
    private static $defaultNumberSet = 'latn';

    public function getPriceFormat($localeCode = null, $currencyCode = null)
    {
        $localeCode = $localeCode ?: $this->_localeResolver->getLocale();
        if ($currencyCode) {
            $currency = $this->currencyFactory->create()->load($currencyCode);
        } else {
            $currency = $this->_scopeResolver->getScope()->getCurrentCurrency();
        }

        $localeData = (new DataBundle())->get($localeCode);
        $defaultSet = $localeData['NumberElements']['default'] ?: self::$defaultNumberSet;

        $format = $localeData['NumberElements'][$defaultSet]['patterns']['currencyFormat']
            ?: ($localeData['NumberElements'][self::$defaultNumberSet]['patterns']['currencyFormat']
                ?: explode(';', $localeData['NumberPatterns'][1])[0]);

        //your main changes are gone here.....
        $decimalSymbol = '.';
        $groupSymbol = ',';

        $pos = strpos($format, ';');
        if ($pos !== false) {
            $format = substr($format, 0, $pos);
        }
        $format = preg_replace("/[^0\#\.,]/", "", $format);
        $totalPrecision = 0;
        $decimalPoint = strpos($format, '.');
        if ($decimalPoint !== false) {
            $totalPrecision = strlen($format) - (strrpos($format, '.') + 1);
        } else {
            $decimalPoint = strlen($format);
        }
        $requiredPrecision = $totalPrecision;
        $t = substr($format, $decimalPoint);
        $pos = strpos($t, '#');
        if ($pos !== false) {
            $requiredPrecision = strlen($t) - $pos - $totalPrecision;
        }

        if (strrpos($format, ',') !== false) {
            $group = $decimalPoint - strrpos($format, ',') - 1;
        } else {
            $group = strrpos($format, '.');
        }
        $integerRequired = strpos($format, '.') - strpos($format, '0');

        $result = [
            //TODO: change interface
            'pattern' => $currency->getOutputFormat(),
            'precision' => $totalPrecision,
            'requiredPrecision' => $requiredPrecision,
            'decimalSymbol' => $decimalSymbol,
            'groupSymbol' => $groupSymbol,
            'groupLength' => $group,
            'integerRequired' => $integerRequired,
        ];       
        return $result;
    }
}

Thanks.


Use below code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->create('\Magento\Framework\Pricing\PriceCurrencyInterface')->format('999,00',true,0);

Format function as below:

public function format(
        $amount,
        $includeContainer = true,
        $precision = self::DEFAULT_PRECISION,
        $scope = null,
        $currency = null
    );

If $includeContainer = true then price will show with span container

<span class="price">$999</span>

$precision = self::DEFAULT_PRECISION It will display two decimal point. Using 0 it will not display decimal point.


By default of Magento 2, price format is a little strange for some currencies so we need to change it. Here’s the way to change price format.

Here’s the example for the case of Vietnamese dong. Default displayed format was 100.000,00. Then I changed it into 100,000 (separated by comma without decimal point).

# vendor/magento/zendframework1/library/Zend/Locale/Data/vi.xml
<symbols numbersystem="latn">
 <decimal>.</decimal>
 <group>,</group>
</symbols>

# vendor/magento/framework/Pricing/PriceCurrencyInterface.php
const DEFAULT_PRECISION = 0

# vendor/magento/module-directory/Model/Currency.php
return $this->formatPrecision($price, 0, $options, $includeContainer, $addBrackets);

# vendor/magento/module-sales/Model/Order.php
public function formatPrice($price, $addBrackets = false)
{
    return $this->formatPricePrecision($price, 0, $addBrackets);
}

Thanks Enjoy :)