Allow thousands separator in formatted numeric strings to be handled as numbers by the Calculation Engine

This commit is contained in:
MarkBaker
2022-11-25 20:03:48 +01:00
parent e1514ac24c
commit 04be65aee8
2 changed files with 7 additions and 3 deletions
@@ -9,7 +9,6 @@ class FormattedNumber
{
/** Constants */
/** Regular Expressions */
// Fraction
private const STRING_REGEXP_FRACTION = '~^\s*(-?)((\d*)\s+)?(\d+\/\d+)\s*$~';
private const STRING_REGEXP_PERCENT = '~^(?:(?: *(?<PrefixedSign>[-+])? *\% *(?<PrefixedSign2>[-+])? *(?<PrefixedValue>[0-9]+\.?[0-9*]*(?:E[-+]?[0-9]*)?) *)|(?: *(?<PostfixedSign>[-+])? *(?<PostfixedValue>[0-9]+\.?[0-9]*(?:E[-+]?[0-9]*)?) *\% *))$~i';
@@ -46,8 +45,10 @@ class FormattedNumber
*/
public static function convertToNumberIfNumeric(string &$operand): bool
{
if (is_numeric($operand)) {
$operand = (float) $operand;
$value = preg_replace(['/(\d),(\d)/u', '/([+-])\s+(\d)/u'], ['$1$2', '$1$2'], trim($operand));
if (is_numeric($value)) {
$operand = (float) $value;
return true;
}
@@ -25,6 +25,9 @@ class FormattedNumberTest extends TestCase
[-12.5, '-12.5'],
[-125.0, '-1.25e2'],
[0.125, '12.5%'],
[1234.5, '1,234.5'],
[-1234.5, '- 1,234.5'],
[1234.5, '+ 1,234.5'],
];
}