diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 4cda2a32c..b605cff6e 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -217,7 +217,7 @@ class NumberFormat extends Supervisor * * @return $this */ - public function setFormatCode($formatCode) + public function setFormatCode(string $formatCode) { if ($formatCode == '') { $formatCode = self::FORMAT_GENERAL; @@ -251,11 +251,11 @@ class NumberFormat extends Supervisor /** * Set Built-In Format Code. * - * @param int $formatCodeIndex + * @param int $formatCodeIndex Id of the built-in format code to use * * @return $this */ - public function setBuiltInFormatCode($formatCodeIndex) + public function setBuiltInFormatCode(int $formatCodeIndex) { if ($this->isSupervisor) { $styleArray = $this->getStyleArray(['formatCode' => self::builtInFormatCode($formatCodeIndex)]); diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php index 5d2d1ddf1..de3666ae1 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php +++ b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php @@ -170,9 +170,11 @@ class Formatter $format = (string) preg_replace('/_.?/ui', ' ', $format); // Let's begin inspecting the format and converting the value to a formatted string - // Check for date/time characters (not inside quotes) - if (preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format, $matches)) { + if ( + (preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format)) && + (preg_match('/0(?![^\[]*\])/miu', $format) === 0) + ) { // datetime format $value = DateFormatter::format($value, $format); } else { diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index 1d7b917d7..da6d2778f 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -1464,4 +1464,19 @@ return [ 'truncate to 1 decimal' => ['009 7.1', 97.13, '000 0.0'], 'scientific many decimal positions truncated' => ['000 0.000 00', 1e-7, '000 0.000 00'], 'scientific very many decimal positions truncated' => ['000 0.000 00', 1e-17, '000 0.000 00'], + [ + '€ 1,111.12 ', + '1111.119', + '[$€-nl_NL]_(#,##0.00_);[$€-nl_NL] (#,##0.00)', + ], + [ + '€ (1,111.12)', + '-1111.119', + '[$€-nl_NL]_(#,##0.00_);[$€-nl_NL] (#,##0.00)', + ], + [ + '€ 1,111.12 ', + '1111.119', + '[$€-en_US]_(#,##0.00_);[$€-en_US] (#,##0.00)', + ], ];