diff --git a/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php b/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php index c1808e28cb..c035345865 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php +++ b/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php @@ -211,7 +211,10 @@ public static function format($value, string $format): string $useThousands = self::areThousandsRequired($format); $scale = self::scaleThousandsMillions($format); - if (preg_match('/#?.*\?\/(\?+|\d+)/', $format)) { + if (preg_match('/[#\?0]?.*[#\?0]\/(\?+|\d+|#)/', $format)) { + // It's a dirty hack; but replace # and 0 digit placeholders with ? + $format = (string) preg_replace('/[#0]+\//', '?/', $format); + $format = (string) preg_replace('/\/[#0]+/', '/?', $format); $value = FractionFormatter::format($value, $format); } else { // Handle the number itself diff --git a/tests/data/Style/NumberFormatFractions.php b/tests/data/Style/NumberFormatFractions.php index 25e094e18c..e7d4cfa69b 100644 --- a/tests/data/Style/NumberFormatFractions.php +++ b/tests/data/Style/NumberFormatFractions.php @@ -9,6 +9,26 @@ 5.25, '# ???/???', ], + [ + '5 2/8', + 5.25, + '# ???/8', + ], + [ + '5 4/16', + 5.25, + '# ???/16', + ], + [ + '5 1/4', + 5.25, + '# ##0/000', + ], + [ + '5 1/4', + 5.25, + '# ##0/###', + ], [ '5 3/10', 5.2999999999999998,