mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-31 12:40:00 +00:00
Allow thousands separator in formatted numeric strings to be handled as numbers by the Calculation Engine
This commit is contained in:
@@ -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'],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user