Skip to content

Commit

Permalink
Fix calculated fields doesn't handle numeric values with non US locale
Browse files Browse the repository at this point in the history
Fixes #5511

When using non US locale and using a mathematical caluclation it returns the string in the wrong number format, which on save will result in incorrect calculations.

Example calc:

`multiply({P0};{P1})`

if P0 is 0.8 and P1 is 0.5 the calculated field will set the string `"0.4" ` to the selected field. If this is a numeric field on save it will parse the result. In german local the dot is the thousand separator, resulting the field beeing saved as 4 and not 0.4 in the database.
  • Loading branch information
attrib authored Jul 10, 2023
1 parent 558c1a1 commit c41a784
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions modules/AOW_Actions/FormulaCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,31 +359,31 @@ private function evaluateNode($text, $childItems = array())

// Mathematical calculations
if (($params = $this->evaluateFunctionParams("add", $text, $childItems)) != null) {
return $this->parseFloat($params[0]) + $this->parseFloat($params[1]);
return $this->format_number($this->parseFloat($params[0]) + $this->parseFloat($params[1]));
}

if (($params = $this->evaluateFunctionParams("subtract", $text, $childItems)) != null) {
return $this->parseFloat($params[0]) - $this->parseFloat($params[1]);
return $this->format_number($this->parseFloat($params[0]) - $this->parseFloat($params[1]));
}

if (($params = $this->evaluateFunctionParams("multiply", $text, $childItems)) != null) {
return $this->parseFloat($params[0]) * $this->parseFloat($params[1]);
return $this->format_number($this->parseFloat($params[0]) * $this->parseFloat($params[1]));
}

if (($params = $this->evaluateFunctionParams("divide", $text, $childItems)) != null) {
return $this->parseFloat($params[0]) / $this->parseFloat($params[1]);
return $this->format_number($this->parseFloat($params[0]) / $this->parseFloat($params[1]));
}

if (($params = $this->evaluateFunctionParams("power", $text, $childItems)) != null) {
return pow($this->parseFloat($params[0]), $this->parseFloat($params[1]));
return $this->format_number(pow($this->parseFloat($params[0]), $this->parseFloat($params[1])));
}

if (($params = $this->evaluateFunctionParams("squareRoot", $text, $childItems)) != null) {
return sqrt($this->parseFloat($params[0]));
return $this->format_number(sqrt($this->parseFloat($params[0])));
}

if (($params = $this->evaluateFunctionParams("absolute", $text, $childItems)) != null) {
return abs($this->parseFloat($params[0]));
return $this->format_number(abs($this->parseFloat($params[0])));
}

// Date functions
Expand Down Expand Up @@ -646,6 +646,18 @@ private function parseFloat($value)
return (float)str_replace(",", ".", $value);
}

private function format_number($value)
{
static $separators = null;
if ($separators === null) {
$separators = get_number_separators();
}
$decimals = strlen(substr(strrchr($value, "."), 1));
// Number needs to be formatted for user, otherwise "0.4" will be converted to 4
// when user has german language setting, where . is thousand separator
return number_format($value, $decimals, $separators[1], $separators[0]);
}

/**
* @param $format
* @param $datestring
Expand Down

0 comments on commit c41a784

Please sign in to comment.