NumericUtils

Common operations and tools related with numbers

package

Default

Methods

Strictly check that the provided value is numeric or throw an exception

forceNumeric(mixed $value,string $valueName = '',string $errorMessage = 'must be numeric'): void
static
Throws
\InvalidArgumentException

If the check fails

Arguments

$value

mixed

A value to check

$valueName

string

The name of the value to be shown at the beginning of the exception message

$errorMessage

string

The rest of the exception message

Strictly check that the provided value is a positive integer or throw an exception

forcePositiveInteger(mixed $value,string $valueName = '',string $errorMessage = 'must be a positive integer'): void
static
Throws
\InvalidArgumentException

If the check fails

Arguments

$value

mixed

A value to check

$valueName

string

The name of the value to be shown at the beginning of the exception message

$errorMessage

string

The rest of the exception message

Generate a random integer between the specified range (both extremes are included).

generateRandomInteger(integer $min,integer $max): integer
static
Throws
\UnexpectedValueException

if max is equal or less than min.

Arguments

$min

integer

lowest possible value (negative values are allowed)

$max

integer

highest possible value (negative values are allowed)

Response

integer

A random integer value between min and max

Get the number represented by the given value

getNumeric(mixed $value,string $decimalDivider = ''): \org\turbocommons\src\main\php\utils\number
static

Arguments

$value

mixed

A value to convert to a number

$decimalDivider

string

The decimal divider to use. Possible values are '.' and ','. If not provided, it will be auto-detected.

Response

\org\turbocommons\src\main\php\utils\number

The numeric type representation from the given value. For example, a string '0001' will return 1

Tells if the given value is a numeric integer or not

isInteger(mixed $value): boolean
static

Arguments

$value

mixed

A value to check

Response

boolean

true if the given value is a numeric integer or represents a numeric integer value, false otherwise

Checks if the given value is numeric.

isNumeric(mixed $value,string $decimalDivider = ''): boolean
static

Arguments

$value

mixed

A value to check.

$decimalDivider

string

The decimal divider to use. Possible values are '.' and ','. If not provided, it will be auto-detected.

Response

boolean

true if the given value is numeric, false otherwise.

Constants

Defines the error message for an exception when a non-numeric value is detected.

NON_NUMERIC_ERROR
constant

string