Skip to content

Commit

Permalink
Bug fixes + added ceil option and trailing_zeros parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
leigeber committed Mar 11, 2013
1 parent f0cd17a commit 7294abc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@ Use Math to execute PHP supported math formulas in ExpressionEngine.
Parameters:

formula = '(5 * 2) / [1]' // math formula (required) supports the following operators as well as bitwise + - * / % ++ -- < > <= => != <> ==

params = '{var}|{var2}' // pipe delimited list of numeric parameters to be replaced into formula, recommended due to use of PHP eval (default: null)

decimals = '2' // sets the number of decimal points (default: "0")

decimal_point = '.' // sets the separator for the decimal point (default: ".")

thousands_seperator = ',' // sets the thousands separator; (default: ",")

absolute = 'yes' // return the absolute number of the result (defaults: "no")
round = 'up|down' // whether to round the result up or down (defaults: no rounding)

round = 'up|down|ceil' // whether to round the result up or down (defaults: no rounding)

numeric_error = 'Error' // message returned when non-numeric parameters are provided (default: "Invalid input")

trailing_zeros = 'yes' // include trailing 0 decimal places (defaults: "no")

Usage:

{exp:math formula="10 - 12" absolute="yes"} outputs 2

{exp:math formula="((4 * 5) / 2)" decimals="2"} outputs 10.00

{exp:math formula="([1] + 1) / [2]" params="{total_results}|2" round="down"} outputs 5 where {total_results} is 10

{exp:math formula="2/3" decimals="2" round="up"} outputs 0.67
73 changes: 53 additions & 20 deletions pi.math.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$plugin_info = array (
'pi_name' => 'Math',
'pi_version' => '1.1.1',
'pi_version' => '1.2.0',
'pi_author' => 'Michael Leigeber',
'pi_author_url' => 'http://www.caddis.co',
'pi_description' => 'Use Math to execute PHP supported math formulas.',
Expand Down Expand Up @@ -30,6 +30,7 @@ function __construct()

// Replace parameters
$params = $this->EE->TMPL->fetch_param('params');
$numeric_error = $this->EE->TMPL->fetch_param('numeric_error', 'Invalid input');

if ($params)
{
Expand All @@ -41,6 +42,13 @@ function __construct()
if (!is_numeric($param))
{
$param = preg_replace('/\D/', '', $param);

if (!is_numeric($param))
{
$this->return_data = $numeric_error;

return;
}
}

$formula = str_replace('[' . $i . ']', $param, $formula);
Expand All @@ -55,43 +63,66 @@ function __construct()
@eval("\$result = $formula;");

// Get settings
$round = $this->EE->TMPL->fetch_param('round');
$decimals = $this->EE->TMPL->fetch_param('decimals', 0);
$round = $this->EE->TMPL->fetch_param('round', false);
$decimals = $this->EE->TMPL->fetch_param('decimals', false);
$decimal_point = $this->EE->TMPL->fetch_param('dec_point', '.');
$thousands_seperator = $this->EE->TMPL->fetch_param('thousands_seperator', ',');
$absolute = $this->EE->TMPL->fetch_param('absolute');
$trailing_zeros = $this->EE->TMPL->fetch_param('trailing_zeros', false);

// Rounding
if ($round == 'up')
$decimal_digits = 0;

// Absolute value
if ($absolute)
{
$result = ceil($result);
$result = abs($result);
}
else if ($round == 'down')

// Rounding
if ($decimals !== false)
{
$result = floor($result);
$mult = pow(10, $decimals);

switch ($round)
{
case 'up':
$result = round($result, $decimals, PHP_ROUND_HALF_UP);
break;
case 'ceil':
$result = ceil($result * $mult) / $mult;
break;
default:
$result = intval($result * $mult) / $mult;
}
}

$parts = explode('.', $result);

$decimal_value = count($parts) > 1 ? $parts[1] : false;
$decimal_digits = strlen($decimal_value);

// Format response
if (strpos($result, '.'))
if ($decimals !== false)
{
$parts = explode('.', $result);

$result = number_format((int)$parts[0], 0, $decimal_point, $thousands_seperator);

if ($decimals > 0)
{
$result .= '.' . str_pad($parts[1], $decimals, 0);
if ($decimal_digits < $decimals && $trailing_zeros)
{
$result .= '.' . str_pad($decimal_value, $decimals, 0);
}
else
{
$result .= $decimal_value ? '.' . $decimal_value : '';
}
}
}
else
{
$result = number_format((int)$result, $decimals, $decimal_point, $thousands_seperator);
}
$decimals = $decimal_digits;

// Absolute value
if ($absolute)
{
$result = abs($result);
$result = number_format((float)$result, $decimals, $decimal_point, $thousands_seperator);
}
}
}
Expand All @@ -111,14 +142,16 @@ function usage()
decimal_point = '.' // sets the separator for the decimal point (default: ".")
thousands_seperator = ',' // sets the thousands separator; (default: ",")
absolute = 'yes' // return the absolute number of the result (defaults: "no")
round = 'up|down' // whether to round the result up or down (defaults: no rounding)
round = 'up|down|ceil' // whether to round the result up or down, where up is standard rounding (defaults: no rounding)
numeric_error = 'Error' // message returned when non-numeric parameters are provided (default: "Invalid input")
trailing_zeros = 'yes' // include trailing 0 decimal places (defaults: "no")

Usage:

{exp:math formula="10 - 12" absolute="yes"} outputs 2
{exp:math formula="((4 * 5) / 2)" decimals="2"} outputs 10.00
{exp:math formula="([1] + 1) / [2]" params="{total_results}|2" round="down"} outputs 5 where {total_results} is 10
{exp:math formula="2/3" decimals="2" round="up"} outputs 0.67
<?php
$buffer = ob_get_contents();

Expand Down

0 comments on commit 7294abc

Please sign in to comment.