Files
PhpSpreadsheet/tests/data/Style/NumberFormatFractions.php
T
ybai08 086ee2c750 Write Integers With a Fraction Format As Excel Does
Fix #3625. With a fraction format, PhpSpreadsheet wrote an integer as
the integer alone, so 1 formatted with `0/0` was `1`. Excel writes
`1/1`: a format with no integer part always shows a fraction. Excel
also shows the fraction when the last numerator placeholder is 0, so 1
formatted with `0 0/0` is `1 0/1`; with a `?` or `#` numerator
placeholder the fraction is left out, as before.

The `#` and `0` placeholders were replaced by `?` before
FractionFormatter saw the format, so the replacement moves into
FractionFormatter, which first notes whether the numerator ends in 0.
2026-10-17 20:21:18 -05:00

198 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
// value, format, result
return [
// Fraction
[
'5 1/4',
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,
'# ???/???',
],
// Vulgar Fraction
[
'21/4',
5.25,
'???/???',
],
[
'0 3/4',
0.75,
'0??/???',
],
[
'3/4',
0.75,
'#??/???',
],
[
' 3/4',
0.75,
'? ??/???',
],
[
' 3/4',
'0.75000',
'? ??/???',
],
[
'5 1/16',
5.0625,
'? ??/???',
],
[
'- 5/8',
-0.625,
'? ??/???',
],
[
'0',
0,
'? ??/???',
],
[
'0',
'0.000',
'? ??/???',
],
[
'-16',
'-016.0',
'? ??/???',
],
// Fixed base Fraction
[
'5 1/2',
5.25,
'# ???/2',
],
[
'5 1/4',
5.25,
'# ???/4',
],
[
'5 2/8',
5.25,
'# ???/8',
],
[
'5 3/10',
5.25,
'# ???/10',
],
[
'5 25/100',
5.25,
'# ???/100',
],
[
'525/100',
5.25,
'??/100',
],
[
'525/100',
5.25,
'???/100',
],
[
'2 2/3',
2.65,
'# ???/3',
],
[
'2 3/4',
2.65,
'# ???/4',
],
[
'2 4/6',
2.65,
'# ???/6',
],
[
'2 8/12',
2.65,
'# ???/12',
],
// Integer values (issue 3625)
'integer in vulgar fraction' => [
'1/1',
1,
'0/0',
],
'integer in vulgar fraction with ? placeholders' => [
'1/1',
1,
'??/???',
],
'larger integer in vulgar fraction' => [
'12/1',
12,
'?/?',
],
'negative integer in vulgar fraction' => [
'-3/1',
-3,
'?/?',
],
'zero in vulgar fraction' => [
'0/1',
0,
'0/0',
],
'integer in vulgar fraction with fixed denominator' => [
'500/100',
5,
'??/100',
],
'integer with 0 numerator placeholder' => [
'1 0/1',
1,
'0 0/0',
],
'integer with 0 numerator placeholder and fixed denominator' => [
'5 0/4',
5,
'0 0/4',
],
'integer with ? numerator placeholder' => [
'1',
1,
'? ??/???',
],
'integer with # whole part and ? numerator placeholder' => [
'7',
7,
'# ?/?',
],
];