mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-17 21:46:37 +00:00
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:
@@ -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' => ['%', '%'],
|
||||
|
||||
Reference in New Issue
Block a user