Merge pull request #3401 from PHPOffice/NumberFormatter-Improved-Fractions

Allow use of # and 0 digit placeholders in fraction masks
This commit is contained in:
Mark Baker
2023-02-25 13:11:20 +01:00
committed by GitHub
2 changed files with 24 additions and 1 deletions
@@ -211,7 +211,10 @@ class NumberFormatter
$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
@@ -9,6 +9,26 @@ return [
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,