MathUtility
Class with helper functions for mathematical calculations
Table of Contents
Methods
- calculateWithParentheses() : string
- Calculates the input with parenthesis levels
- calculateWithPriorityToAdditionAndSubtraction() : float|string
- Calculates the input by +,-,*,/,%,^ with priority to + and -
- canBeInterpretedAsFloat() : bool
- Tests if the input can be interpreted as float.
- canBeInterpretedAsInteger() : bool
- Tests if the input can be interpreted as integer.
- forceIntegerInRange() : int
- Forces the integer $theInt into the boundaries of $min and $max. If the $theInt is FALSE then the $defaultValue is applied.
- isIntegerInRange() : bool
- Checks whether the given number $value is an integer in the range [$minimum;$maximum]
- roundDecimalString() : string
- Round a decimal number given as string to $scale decimal digits and return it as string, padded with zeros if it has less decimal digits. Values exactly in the middle are rounded away from zero, a result of zero is never negative.
Methods
calculateWithParentheses()
Calculates the input with parenthesis levels
public
static calculateWithParentheses(string $string) : string
Parameters
- $string : string
-
Input string, eg "(123 + 456) / 789 - 4
Tags
Return values
string —Calculated value. Or error string.
calculateWithPriorityToAdditionAndSubtraction()
Calculates the input by +,-,*,/,%,^ with priority to + and -
public
static calculateWithPriorityToAdditionAndSubtraction(string $string) : float|string
Parameters
- $string : string
-
Input string, eg "123 + 456 / 789 - 4
Tags
Return values
float|string —Calculated value. Or error string.
canBeInterpretedAsFloat()
Tests if the input can be interpreted as float.
public
static canBeInterpretedAsFloat(mixed $var) : bool
Note: Float casting from objects or arrays is considered undefined and thus will return false.
Parameters
- $var : mixed
-
Any input variable to test
Tags
Return values
bool —Returns TRUE if string is a float
canBeInterpretedAsInteger()
Tests if the input can be interpreted as integer.
public
static canBeInterpretedAsInteger(mixed $var) : bool
Note: "0" will return true while any other number with a leading 0 (including multiple zeroes) will be false.
Note: Integer casting from objects or arrays is considered undefined and thus will return false.
Parameters
- $var : mixed
-
Any input variable to test
Tags
Return values
bool —Returns TRUE if string is an integer
forceIntegerInRange()
Forces the integer $theInt into the boundaries of $min and $max. If the $theInt is FALSE then the $defaultValue is applied.
public
static forceIntegerInRange(mixed $theInt, int $min[, int $max = 2000000000 ][, int $defaultValue = 0 ]) : int
Parameters
- $theInt : mixed
-
Input value - will be cast to int if non-integer value is passed.
- $min : int
-
Lower limit
- $max : int = 2000000000
-
Higher limit
- $defaultValue : int = 0
-
Default value if input is FALSE.
Return values
int —The input value forced into the boundaries of $min and $max
isIntegerInRange()
Checks whether the given number $value is an integer in the range [$minimum;$maximum]
public
static isIntegerInRange(mixed $value, int $minimum, int $maximum) : bool
Parameters
- $value : mixed
-
Integer value to check. If not an integer this method always returns false.
- $minimum : int
-
Lower boundary of the range
- $maximum : int
-
Upper boundary of the range
Return values
boolroundDecimalString()
Round a decimal number given as string to $scale decimal digits and return it as string, padded with zeros if it has less decimal digits. Values exactly in the middle are rounded away from zero, a result of zero is never negative.
public
static roundDecimalString(string $value, int $scale) : string
The calculation is done on the digits and not on a float, since a float can only hold about 15 significant digits: number_format(12345678.1234567891, 10) returns "12345678.1234567892", while this method keeps the given digits.
Parameters
- $value : string
-
A number like "-1234.5678" or "1.0e-7": digits with an optional sign, decimal point and exponent. An empty string is treated as zero, since that is how an empty form field arrives here. Anything else, a decimal comma for instance, is rejected instead of being silently reinterpreted.
- $scale : int
-
Number of decimal digits of the result, negative values are treated as zero