Automattic\WooCommerce\StoreApi\Utilities

QuantityLimits::limit_to_multiplepublicWC 1.0

Return a number using the closest multiple of another number. Used to enforce step/multiple values.

Method of the class: QuantityLimits{}

No Hooks.

Returns

Int|float.

Usage

$QuantityLimits = new QuantityLimits();
$QuantityLimits->limit_to_multiple( $number, $multiple_of, string $rounding_= 'round' );
$number(int|float) (required)
Number to round.
$multiple_of(int|float) (required)
The multiple.
$rounding_function(string)
ceil, floor, or round.
Default: 'round'

QuantityLimits::limit_to_multiple() code WC 10.9.1

public function limit_to_multiple( $number, $multiple_of, string $rounding_function = 'round' ) {
	// Handle edge cases.
	$number      = NumberUtil::normalize( $number, null );
	$multiple_of = NumberUtil::normalize( $multiple_of, null );

	if ( is_null( $multiple_of ) || is_null( $number ) ) {
		return 0;
	}

	if ( 0 >= $multiple_of || $this->is_multiple_of( $number, $multiple_of ) ) {
		return $number;
	}

	// Ensure valid rounding function.
	$rounding_function = in_array( $rounding_function, [ 'ceil', 'floor', 'round' ], true ) ? $rounding_function : 'round';

	return NumberUtil::normalize( $rounding_function( $number / $multiple_of ) * $multiple_of );
}