To convert currency you can use Magento\Framework\Pricing\PriceCurrencyInterface
.
Module’s block class:
<?php
namespace VendorName\ModuleName\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $_storeManager;
protected $_currency;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Directory\Model\Currency $currency,
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrencyInterface,
array $data = []
)
{
$this->_storeManager = $storeManager;
$this->_currency = $currency;
$this->_priceCurrencyInterface = $priceCurrencyInterface;
parent::__construct($context, $data);
}
/**
* Covert currency code
*
* @return It return price according to current store from base currency
*/
public function convertPrice($amount,$currency)
{
$store = $this->_storeManager->getStore()->getStoreId();
return $priceCurrencyObject->convert($amount, $store, $currency);
}
}
?>
To get other currncy info storewise:
// retutn Currency Symbol:
$this->_currency->getCurrencySymbol();
// return store base currency code
$this->_storeManager->getStore()->getBaseCurrencyCode();
// return default store currency code
$this->_storeManager->getStore()->getDefaultCurrencyCode();
// return array of installed currencies for the scope
$this->_storeManager->getStore()->getAllowedCurrencies();
// return current currency rate
$this->_storeManager->getStore()->getCurrentCurrencyRate();
Template (.phtml) file:
echo $block->convertPrice($amount,$currency);
Hope this helps!