Merge pull request #3210 from PHPOffice/CalcEngine-FormattedNumbers_Thousands-Separator-in-Percentages

Allow thousands separator in percentage formatted strings in formulae
This commit is contained in:
Mark Baker
2022-11-25 19:34:18 +01:00
committed by GitHub
2 changed files with 4 additions and 1 deletions
@@ -83,8 +83,10 @@ class FormattedNumber
*/
public static function convertToNumberIfPercent(string &$operand): bool
{
$value = preg_replace('/(\d),(\d)/u', '$1$2', $operand);
$match = [];
if (preg_match(self::STRING_REGEXP_PERCENT, $operand, $match, PREG_UNMATCHED_AS_NULL)) {
if ($value !== null && preg_match(self::STRING_REGEXP_PERCENT, $value, $match, PREG_UNMATCHED_AS_NULL)) {
//Calculate the percentage
$sign = ($match['PrefixedSign'] ?? $match['PrefixedSign2'] ?? $match['PostfixedSign']) ?? '';
$operand = (float) ($sign . ($match['PostfixedValue'] ?? $match['PrefixedValue'])) / 100;
@@ -87,6 +87,7 @@ class FormattedNumberTest extends TestCase
'trailing percent with space' => ['0.02', '2 %'],
'trailing percent with leading and trailing space' => ['0.02', ' 2 % '],
'leading percent with decimals' => ['0.025', ' % 2.5'],
'Percentage with thousands separator' => ['12.345', ' % 1,234.5'],
//These should all fail
'percent only' => ['%', '%'],