Skip to content

Commit 69e942a

Browse files
authored
Merge pull request #3401 from PHPOffice/NumberFormatter-Improved-Fractions
Allow use of # and 0 digit placeholders in fraction masks
2 parents 324e190 + 0ebbb32 commit 69e942a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ public static function format($value, string $format): string
211211
$useThousands = self::areThousandsRequired($format);
212212
$scale = self::scaleThousandsMillions($format);
213213

214-
if (preg_match('/#?.*\?\/(\?+|\d+)/', $format)) {
214+
if (preg_match('/[#\?0]?.*[#\?0]\/(\?+|\d+|#)/', $format)) {
215+
// It's a dirty hack; but replace # and 0 digit placeholders with ?
216+
$format = (string) preg_replace('/[#0]+\//', '?/', $format);
217+
$format = (string) preg_replace('/\/[#0]+/', '/?', $format);
215218
$value = FractionFormatter::format($value, $format);
216219
} else {
217220
// Handle the number itself

tests/data/Style/NumberFormatFractions.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@
99
5.25,
1010
'# ???/???',
1111
],
12+
[
13+
'5 2/8',
14+
5.25,
15+
'# ???/8',
16+
],
17+
[
18+
'5 4/16',
19+
5.25,
20+
'# ???/16',
21+
],
22+
[
23+
'5 1/4',
24+
5.25,
25+
'# ##0/000',
26+
],
27+
[
28+
'5 1/4',
29+
5.25,
30+
'# ##0/###',
31+
],
1232
[
1333
'5 3/10',
1434
5.2999999999999998,

0 commit comments

Comments
 (0)