Automattic\WooCommerce\Internal\Admin\Settings

SettingsUISchema::normalize_decimal_string │ private static │ WC 1.0

Normalize a decimal string to its significant digits and base-ten power.

Метод класса: SettingsUISchema{}

Хуков нет.

Возвращает

array{string, int}|null. Normalized signed digits and power, or null when invalid.

Использование

$result = SettingsUISchema::normalize_decimal_string( $value ): ?array;
$value(строка) (обязательный)
Decimal value.

Код SettingsUISchema::normalize_decimal_string() WC 11.2.2

private static function normalize_decimal_string( string $value ): ?array {
	if ( ! preg_match( self::DECIMAL_PATTERN, $value, $matches ) ) {
		return null;
	}

	$whole    = $matches['whole'] ?? '';
	$fraction = '' !== ( $matches['fraction'] ?? '' ) ? $matches['fraction'] : ( $matches['bare_fraction'] ?? '' );
	$digits   = ltrim( $whole . $fraction, '0' );
	if ( '' === $digits ) {
		return array( '0', 0 );
	}

	$exponent        = $matches['exponent'] ?? '0';
	$exponent_digits = ltrim( $exponent, '+-0' );
	if ( 6 < strlen( $exponent_digits ) ) {
		return null;
	}

	$power          = (int) $exponent - strlen( $fraction );
	$trimmed_digits = rtrim( $digits, '0' );
	$power         += strlen( $digits ) - strlen( $trimmed_digits );
	$signed_digits  = '-' === $matches['sign'] ? '-' . $trimmed_digits : $trimmed_digits;

	return array( $signed_digits, $power );
}