Allow currency numeric strings with thousands separator

This commit is contained in:
MarkBaker
2022-11-25 13:01:13 +01:00
parent 66da98ebb8
commit 171d7684d6
2 changed files with 23 additions and 14 deletions
@@ -104,10 +104,12 @@ class FormattedNumber
public static function convertToNumberIfCurrency(string &$operand): bool
{
$quotedCurrencyCode = preg_quote(StringHelper::getCurrencyCode());
$regExp = '~^(?:(?: *(?<PrefixedSign>[-+])? *' . $quotedCurrencyCode . ' *(?<PrefixedSign2>[-+])? *(?<PrefixedValue>[0-9]+\.?[0-9*]*(?:E[-+]?[0-9]*)?) *)|(?: *(?<PostfixedSign>[-+])? *(?<PostfixedValue>[0-9]+\.?[0-9]*(?:E[-+]?[0-9]*)?) *' . $quotedCurrencyCode . ' *))$~i';
$value = preg_replace('/(\d),(\d)/u', '$1$2', $operand);
$regExp = '~^(?:(?: *(?<PrefixedSign>[-+])? *' . $quotedCurrencyCode . ' *(?<PrefixedSign2>[-+])? *(?<PrefixedValue>[0-9]+\.?[0-9*]*(?:E[-+]?[0-9]*)?) *)|(?: *(?<PostfixedSign>[-+])? *(?<PostfixedValue>[0-9]+\.?[0-9]*(?:E[-+]?[0-9]*)?) *' . $quotedCurrencyCode . ' *))$~ui';
$match = [];
if (preg_match($regExp, $operand, $match, PREG_UNMATCHED_AS_NULL)) {
if ($value !== null && preg_match($regExp, $value, $match, PREG_UNMATCHED_AS_NULL)) {
//Determine the sign
$sign = ($match['PrefixedSign'] ?? $match['PrefixedSign2'] ?? $match['PostfixedSign']) ?? '';
//Cast to a float
@@ -204,24 +204,31 @@ class FormattedNumberTest extends TestCase
public function providerCurrencies(): array
{
$currencyCode = StringHelper::getCurrencyCode();
return [
'basic_prefix_currency' => ['2.75', "{$currencyCode}2.75"],
'basic_postfix_currency' => ['2.75', "2.75{$currencyCode}"],
'basic_prefix_currency' => ['2.75', "{$currencyCode}2.75"],
'basic_postfix_currency' => ['2.75', "2.75{$currencyCode}"],
'basic_prefix_currency_with_spaces' => ['2.75', "{$currencyCode} 2.75"],
'basic_postfix_currency_with_spaces' => ['2.75', "2.75 {$currencyCode}"],
'basic_prefix_currency_with_spaces' => ['2.75', "{$currencyCode} 2.75"],
'basic_postfix_currency_with_spaces' => ['2.75', "2.75 {$currencyCode}"],
'negative_basic_prefix_currency' => ['-2.75', "-{$currencyCode}2.75"],
'negative_basic_postfix_currency' => ['-2.75', "-2.75{$currencyCode}"],
'negative_basic_prefix_currency' => ['-2.75', "-{$currencyCode}2.75"],
'negative_basic_postfix_currency' => ['-2.75', "-2.75{$currencyCode}"],
'negative_basic_prefix_currency_with_spaces' => ['-2.75', "-{$currencyCode} 2.75"],
'negative_basic_postfix_currency_with_spaces' => ['-2.75', "-2.75 {$currencyCode}"],
'negative_basic_prefix_currency_with_spaces' => ['-2.75', "-{$currencyCode} 2.75"],
'negative_basic_postfix_currency_with_spaces' => ['-2.75', "-2.75 {$currencyCode}"],
'basic_prefix_scientific_currency' => ['2000000', "{$currencyCode}2E6"],
'basic_postfix_scientific_currency' => ['2000000', "2E6{$currencyCode}"],
'positive_signed_prefix_currency_with_spaces' => ['2.75', "+{$currencyCode} 2.75"],
'positive_signed_prefix_currency_with_spaces-2' => ['2.75', "{$currencyCode} +2.75"],
'positive_signed_postfix_currency_with_spaces' => ['2.75', "+2.75 {$currencyCode}"],
'basic_prefix_scientific_currency_with_spaces' => ['2000000', "{$currencyCode} 2E6"],
'basic_postfix_scientific_currency_with_spaces' => ['2000000', "2E6 {$currencyCode}"],
'basic_prefix_scientific_currency' => ['2000000', "{$currencyCode}2E6"],
'basic_postfix_scientific_currency' => ['2000000', "2E6{$currencyCode}"],
'basic_prefix_scientific_currency_with_spaces' => ['2000000', "{$currencyCode} 2E6"],
'basic_postfix_scientific_currency_with_spaces' => ['2000000', "2E6 {$currencyCode}"],
'high_value_currency_with_thousands_separator' => ['2750000', "+{$currencyCode} 2,750,000"],
];
}
}