DaveChild\TextStatistics\Maths::bcCalc PHP Method

bcCalc() public static method

Do simple reliable floating point calculations without the risk of wrong results
See also: http://floating-point-gui.de/
See also: the big red warning on http://php.net/language.types.float.php
public static bcCalc ( mixed $number1, string $action, mixed $number2, boolean $round = false, integer $decimals, integer $precision = 10 ) : mixed
$number1 mixed Scalar (string/int/float/bool)
$action string Calculation action to execute. Valid input: '+' or 'add' or 'addition', '-' or 'sub' or 'subtract', '*' or 'mul' or 'multiply', '/' or 'div' or 'divide', '%' or 'mod' or 'modulus' '=' or 'comp' or 'compare'
$number2 mixed Scalar (string/int/float/bool)
$round boolean Whether or not to round the result. Defaults to false. Will be disregarded for a compare operation
$decimals integer Decimals for rounding operation. Defaults to 0.
$precision integer Calculation precision. Defaults to 10.
return mixed Calculation result or false if either or the numbers isn't scalar or an invalid operation was passed - for compare the result will always be an integer - for all other operations, the result will either be an integer (preferred) or a float
    public static function bcCalc($number1, $action, $number2, $round = false, $decimals = 0, $precision = 10)
    {
        if (!is_scalar($number1) || !is_scalar($number2)) {
            return false;
        }
        // Check whether bcmath extension is available
        if (is_null(self::$blnBcmath)) {
            self::$blnBcmath = extension_loaded('bcmath');
        }
        // Check values of input variables
        if (self::$blnBcmath) {
            $number1 = strval($number1);
            $number2 = strval($number2);
        }
        // Normalise operator
        $action = self::normaliseOperator($action);
        // Perform calculation
        return self::performCalc($number1, $action, $number2, $round, $decimals, $precision);
    }