diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php index ef756d7b..01407e64 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php +++ b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php @@ -130,7 +130,7 @@ class Formatter // In Excel formats, "_" is used to add spacing, // The following character indicates the size of the spacing, which we can't do in HTML, so we just use a standard space - $format = preg_replace('/_/ui', ' ', $format); + $format = preg_replace('/_.?/ui', ' ', $format); // Let's begin inspecting the format and converting the value to a formatted string diff --git a/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php b/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php index 9a4f32ac..0a94c333 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php +++ b/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; +use PhpOffice\PhpSpreadsheet\Style\NumberFormat; class NumberFormatter { @@ -145,7 +146,6 @@ class NumberFormatter $format = preg_replace('/0,+/', '0', $format); $format = preg_replace('/#,+/', '#', $format); } - if (preg_match('/#?.*\?\/\?/', $format, $m)) { if ($value != (int) $value) { $value = FractionFormatter::format($value, $format); @@ -163,7 +163,12 @@ class NumberFormatter $n = '/\\[[^\\]]+\\]/'; $m = preg_replace($n, '', $format); if (preg_match(self::NUMBER_REGEX, $m, $matches)) { + // There are placeholders for digits, so inject digits from the value into the mask $value = self::formatStraightNumericValue($value, $format, $matches, $useThousands); + } elseif ($format !== NumberFormat::FORMAT_GENERAL) { + // Yes, I know that this is basically just a hack; + // if there's no placeholders for digits, just return the format mask "as is" + $value = str_replace('?', '', $format ?? ''); } } diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index aee357a7..92a38cce 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -131,7 +131,7 @@ return [ 'Spacing Character' => [ '826.00 €', 826, - '#,##0.00 _€', + '#,##0.00 __€', ], [ '5.68', @@ -263,7 +263,7 @@ return [ [ '( 12.30% )', -0.123, - '_0.00%_;( 0.00% )', + '_(0.00%_;( 0.00% )', ], // Fraction [ @@ -374,15 +374,35 @@ return [ '[$-1010409]#,##0.00;-#,##0.00', ], [ - ' ($ 23.06 )', + ' $ 23.06 ', 23.0597, '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)', ], [ - ' (€ 13.03 )', - 13.0316, + ' € (13.03)', + -13.0316, '_("€"* #,##0.00_);_("€"* \(#,##0.00\);_("€"* "-"??_);_(@_)', ], + [ + ' € 11.70 ', + 11.7, + '_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-', + ], + [ + '-€ 12.14 ', + -12.14, + '_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-', + ], + [ + ' € - ', + 0, + '_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-', + ], + [ + 'test', + 'test', + '_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-', + ], // Named colours // Simple color [