Documentation

Currency

BX.Currency library is used to format prices with accounting of current JS language.

Part of the Currency module (currency) .

The following code connects the library:

if (\Bitrix\Main\Loader::includeModule('currency'))
{
	CJSCore::Init(['currency']);
}

Price formatting:

<script type="text/javascript">
var price = 141.56,
	currency = 'USD';

item.innerHTML = BX.Currency.currencyFormat(price, currency, true); // insert formatted price with display template applied
item2.innerHTML = BX.Currency.currencyFormat(price, currency, false); // insert formatted price without template

</script>
/*
Result
item contains string $141.56
item2 contains string 141.56 (without currency symbol and the rest of template features)
*/

Before returning the format, you must clearly define formats for the required currencies:

<?
$currencies = []; // get all the available currencies 
$currencyIterator = \Bitrix\Currency\CurrencyTable::getList([
	'select' => ['CURRENCY']
]);
while ($currency = $currencyIterator->fetch())
{
	$currencyFormat = \CCurrencyLang::GetFormatDescription($currency['CURRENCY']);
	$currencies[] = [
		'CURRENCY' => $currency['CURRENCY'],
		'FORMAT' => [
			'FORMAT_STRING' => $currencyFormat['FORMAT_STRING'],
			'DEC_POINT' => $currencyFormat['DEC_POINT'],
			'THOUSANDS_SEP' => $currencyFormat['THOUSANDS_SEP'],
			'DECIMALS' => $currencyFormat['DECIMALS'],
			'THOUSANDS_VARIANT' => $currencyFormat['THOUSANDS_VARIANT'],
			'HIDE_ZERO' => $currencyFormat['HIDE_ZERO']
		]
	];
}
?>
<script type="text/javascript">
BX.Currency.setCurrencies(<?=CUtil::PhpToJSObject($currencies, false, true, true);?>);
</script>

or asynchronously upload the currencies (currency 19.0.100module version and higher):

<script type="text/javascript">
BX.Currency.loadCurrencyFormat('USD').then(function() 
{
	item.innerHTML = BX.Currency.currencyFormat(123, 'USD', true);
});
</script>

When using the second method you must manage the upload process and not use the formatting method before data is uploaded successfully.

Format description (objet fields) almost completely match the data, returned by the method CCurrencyLang::GetFormatDescription (without the key THOUSANDS_VARIANT).

FunctionDescriptionAvailable from version
BX.Currency.cleanErases formats of all currencies.
BX.Currency.clearCurrencyDeletes a format (if available).
BX.Currency.currencyFormatFormat price.
BX.Currency.getCurrencyFormatReturns currency format.
BX.Currency.loadCurrencyFormatAsynchronous format upload.
BX.Currency.setCurrenciesSets formats for several currencies at once.
BX.Currency.setCurrencyFormatSets a specific currency format.



© «Bitrix24», 2001-2022
Up