Automattic\WooCommerce\Internal\Admin\Settings

SettingsUISchema::get_integral_decimal │ private static │ WC 1.0

Return the exact integral decimal represented by a numeric value.

Method of the class: SettingsUISchema{}

No Hooks.

Returns

string|null. Normalized signed integer, or null when non-integral.

Usage

$result = SettingsUISchema::get_integral_decimal( $value ): ?string;
$value(mixed) (required)
Candidate numeric value.

SettingsUISchema::get_integral_decimal() code WC 11.2.1

private static function get_integral_decimal( $value ): ?string {
	if ( is_int( $value ) ) {
		return (string) $value;
	}

	if ( is_float( $value ) ) {
		return is_finite( $value ) && floor( $value ) === $value ? sprintf( '%.0f', $value ) : null;
	}

	if ( ! is_string( $value ) ) {
		return null;
	}

	$value = trim( $value );
	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' );
	$digits   = '' === $digits ? '0' : $digits;
	$exponent = $matches['exponent'] ?? '0';
	$negative = '-' === $matches['sign'];
	$scale    = strlen( $fraction );

	if ( '0' === $digits ) {
		// Zero stays zero at every exponent, so return before expanding it and
		// tripping the overflow sentinel below on values such as "0e17".
		return '0';
	}

	if ( strlen( ltrim( $exponent, '+-0' ) ) > 6 ) {
		return '-' === substr( $exponent, 0, 1 ) ? null : ( $negative ? '-' : '' ) . str_repeat( '9', 17 );
	}

	$decimal_places = $scale - (int) $exponent;
	if ( 0 < $decimal_places ) {
		if ( $decimal_places >= strlen( $digits ) ) {
			return null;
		}

		$trailing = substr( $digits, -$decimal_places );
		if ( '' !== trim( $trailing, '0' ) ) {
			return null;
		}
		$digits = substr( $digits, 0, -$decimal_places );
	} elseif ( 0 > $decimal_places ) {
		$zeros = -$decimal_places;
		if ( 17 < strlen( $digits ) + $zeros ) {
			$digits = str_repeat( '9', 17 );
		} else {
			$digits .= str_repeat( '0', $zeros );
		}
	}

	$digits = ltrim( $digits, '0' );
	if ( '' === $digits ) {
		return '0';
	}

	return $negative ? '-' . $digits : $digits;
}