Automattic\WooCommerce\Utilities
NumberUtil::normalize
Converts numbers (floats, strings, integers) to numeric values to be safely used in PHP functions like floor() which expect int or float.
Method of the class: NumberUtil{}
No Hooks.
Returns
Int|float|Mixed. Returns the numeric value or the fallback value if conversion fails.
Usage
$result = NumberUtil::normalize( $value, $fallback );
- $value(mixed) (required)
- The value to convert.
- $fallback(mixed)
- The value to return if the conversion fails.
NumberUtil::normalize() NumberUtil::normalize code WC 10.3.6
public static function normalize( $value, $fallback = 0 ) {
// Trim string values to handle whitespace consistently across PHP versions.
if ( is_string( $value ) ) {
$value = trim( $value );
}
if ( is_numeric( $value ) ) {
$numeric_value = is_string( $value ) ? floatval( $value ) : $value;
// Round to precision to avoid floating-point precision issues.
return is_int( $numeric_value ) ? $numeric_value : round( $numeric_value, WC_ROUNDING_PRECISION );
}
return $fallback;
}