CCrmCurrency::MoneyToString

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmCurrency
  4. MoneyToString
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_currency.php
  • Класс: \CCrmCurrency
  • Вызов: CCrmCurrency::MoneyToString
static function MoneyToString($sum, $currencyID, $formatStr = '')
{
	if(!Loader::includeModule('currency'))
	{
		return number_format($sum, 2, '.', '');
	}

	$formatInfo = CCurrencyLang::GetCurrencyFormat($currencyID);
	$formatInfo['DECIMALS'] = isset($formatInfo['DECIMALS']) ?  intval($formatInfo['DECIMALS']) : 2;

	if(!isset($formatInfo['DEC_POINT']))
	{
		$formatInfo['DEC_POINT'] = '.';
	}

	if(!empty($formatInfo['THOUSANDS_VARIANT']))
	{
		$thousands = $formatInfo['THOUSANDS_VARIANT'];

		if($thousands === 'N')
		{
			$formatInfo['THOUSANDS_SEP'] = '';
		}
		elseif($thousands === 'D')
		{
			$formatInfo['THOUSANDS_SEP'] = '.';
		}
		elseif($thousands === 'C')
		{
			$formatInfo['THOUSANDS_SEP'] = ',';
		}
		elseif($thousands === 'S' || $thousands === 'B')
		{
			$formatInfo['THOUSANDS_SEP'] = chr(32);
		}
	}

	if(!isset($formatInfo['THOUSANDS_SEP']))
	{
		$formatInfo['THOUSANDS_SEP'] = '';
	}

	if($sum === '' || filter_var($sum, FILTER_VALIDATE_INT|FILTER_VALIDATE_FLOAT) !== false)
	{
		// Standard format for float
		CCurrencyLang::enableUseHideZero();
		$result = strip_tags(CCurrencyLang::CurrencyFormat($sum, $currencyID, $formatStr !== '#'));
		CCurrencyLang::disableUseHideZero();
		return $result;
	}
	else
	{
		// Do not convert to float to avoid data lost caused by overflow (9 999 999 999 999 999.99 ->10 000 000 000 000 000.00)
		$triadSep = strval($formatInfo['THOUSANDS_SEP']);
		$decPoint = strval($formatInfo['DEC_POINT']);
		$dec = intval($formatInfo['DECIMALS']);

		$sum = str_replace(',', '.', strval($sum));
		$sumArr = explode('.', $sum, 2);
		$i = $sumArr[0] ?? null;
		$d = $sumArr[1] ?? null;

		$len = mb_strlen($i);
		$leadLen = $len % 3;
		if($leadLen === 0)
		{
			$leadLen = 3; //take a first triad
		}
		$lead = mb_substr($i, 0, $leadLen);
		if(!is_string($lead))
		{
			$lead = '';
		}
		$triads = mb_substr($i, $leadLen);
		if(!is_string($triads))
		{
			$triads = '';
		}
		$s = $triads !== '' ? $lead.preg_replace('/(\\d{3})/', $triadSep.'\\1', $triads) : ($lead !== '' ? $lead : '0');
		if($dec > 0)
		{
			$s .= $decPoint.str_pad(mb_substr($d, 0, $dec), $dec, '0', STR_PAD_RIGHT);
		}
	}

	$formatStr = strval($formatStr);
	if($formatStr === '' && ($formatInfo['FORMAT_STRING'] ?? '') !== '')
	{
		$formatStr = $formatInfo['FORMAT_STRING'];
	}

	if($formatStr === '' || $formatStr === '#')
	{
		return strip_tags($s);
	}

	//Skip HTML entities
	return strip_tags(
		preg_replace('/(^|[^&])#/', '${1}'.$s, $formatStr)
	);
}

Добавить комментарий