• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_product_helper.php
  • Класс: \CCrmProductHelper
  • Вызов: CCrmProductHelper::parseFloat
static function parseFloat($value, $precision = null, $defaultValue = 0.0)
{
	$value = strval($value);

	if (!is_double($defaultValue))
	{
		$defaultValue = doubleval($defaultValue);
	}
	if ($defaultValue === null)
		$defaultValue = 0.0;

	if (empty($value))
	{
		return $defaultValue;
	}

	$value = trim($value);
	$dot = mb_strpos($value, '.');
	$comma = mb_strpos($value, ',');
	$isNegative = (mb_substr($value, 0, 1) === '-');

	if ($dot === false && $comma !== false && $comma >= 0)
	{
		$s1 = mb_substr($value, 0, $comma);
		$decimalLength = mb_strlen($value) - $comma - 1;
		if ($decimalLength > 0)
		{
			$s1 .= '.'.mb_substr($value, $comma + 1, $decimalLength);
		}
		$value = $s1;
	}
	$value = preg_replace('/[^\d\.]+/', '', $value);
	$value = doubleval($value);

	if ($value === null)
	{
		$value = $defaultValue;
	}
	if ($isNegative)
	{
		$value = -$value;
	}

	if (is_int($precision) && $precision >= 0)
	{
		$value = round($value, $precision);
	}

	return $value;
}