Skip to content

Commit 51a1d6e

Browse files
authored
Merge pull request #3211 from PHPOffice/CalcEngine-FormattedNumbers_Thousands-Separator
Allow thousands separator in formatted numeric strings
2 parents e1514ac + 04be65a commit 51a1d6e

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/PhpSpreadsheet/Calculation/Engine/FormattedNumber.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class FormattedNumber
99
{
1010
/** Constants */
1111
/** Regular Expressions */
12-
// Fraction
1312
private const STRING_REGEXP_FRACTION = '~^\s*(-?)((\d*)\s+)?(\d+\/\d+)\s*$~';
1413

1514
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 @@ public static function convertToNumberIfFormatted(string &$operand): bool
4645
*/
4746
public static function convertToNumberIfNumeric(string &$operand): bool
4847
{
49-
if (is_numeric($operand)) {
50-
$operand = (float) $operand;
48+
$value = preg_replace(['/(\d),(\d)/u', '/([+-])\s+(\d)/u'], ['$1$2', '$1$2'], trim($operand));
49+
50+
if (is_numeric($value)) {
51+
$operand = (float) $value;
5152

5253
return true;
5354
}

tests/PhpSpreadsheetTests/Calculation/Engine/FormattedNumberTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function providerNumbers(): array
2525
[-12.5, '-12.5'],
2626
[-125.0, '-1.25e2'],
2727
[0.125, '12.5%'],
28+
[1234.5, '1,234.5'],
29+
[-1234.5, '- 1,234.5'],
30+
[1234.5, '+ 1,234.5'],
2831
];
2932
}
3033

0 commit comments

Comments
 (0)