NumberUtil
in package
A class of utilities for dealing with numbers.
Table of Contents
- array_sum() : float
- Get the sum of an array of values using the built-in array_sum function, but sanitize the array values first to ensure they are all floats.
- ceil() : float
- Ceil a number using the built-in `ceil` function.
- floor() : float
- Floor a number using the built-in `floor` function.
- normalize() : int|float|mixed
- Converts numbers (floats, strings, integers) to numeric values to be safely used in PHP functions like floor() which expect int or float.
- round() : float
- Round a number using the built-in `round` function, but unless the value to round is numeric (a number or a string that can be parsed as a number), apply 'floatval' first to it (so it will convert it to 0 in most cases).
- sanitize_cost_in_current_locale() : string
- Sanitize a cost value based on the current locale decimal and thousand separators.
Methods
array_sum()
Get the sum of an array of values using the built-in array_sum function, but sanitize the array values first to ensure they are all floats.
public
static array_sum(array<string|int, mixed> $arr) : float
This is needed because in PHP 8.3 non-numeric values that cannot be cast as an int or a float will cause an E_WARNING to be emitted. Prior to PHP 8.3 these values were just ignored.
Note that, unlike the built-in array_sum, this one will always return a float, never an int.
Parameters
- $arr : array<string|int, mixed>
-
The array of values to sum.
Return values
float —ceil()
Ceil a number using the built-in `ceil` function.
public
static ceil(mixed $val) : float
Parameters
- $val : mixed
-
The value to ceil.
Return values
float —floor()
Floor a number using the built-in `floor` function.
public
static floor(mixed $val) : float
Parameters
- $val : mixed
-
The value to floor.
Return values
float —normalize()
Converts numbers (floats, strings, integers) to numeric values to be safely used in PHP functions like floor() which expect int or float.
public
static normalize(mixed $value, mixed $fallback) : int|float|mixed
Parameters
- $value : mixed
-
The value to convert.
- $fallback : mixed
-
The value to return if the conversion fails.
Return values
int|float|mixed — Returns the numeric value or the fallback value if conversion fails.round()
Round a number using the built-in `round` function, but unless the value to round is numeric (a number or a string that can be parsed as a number), apply 'floatval' first to it (so it will convert it to 0 in most cases).
public
static round(mixed $val, int $precision[, int $mode = PHP_ROUND_HALF_UP ]) : float
This is needed because in PHP 7 applying round
to a non-numeric value returns 0,
but in PHP 8 it throws an error. Specifically, in WooCommerce we have a few places where
round('') is often executed.
Parameters
- $val : mixed
-
The value to round.
- $precision : int
-
The optional number of decimal digits to round to.
- $mode : int = PHP_ROUND_HALF_UP
-
A constant to specify the mode in which rounding occurs.
Return values
float — The value rounded to the given precision as a float.sanitize_cost_in_current_locale()
Sanitize a cost value based on the current locale decimal and thousand separators.
public
static sanitize_cost_in_current_locale(string $value) : string
Parameters
- $value : string
-
The value to sanitize.