Skip to content

Commit 3e8d505

Browse files
authored
Minor Fix for Percentage Formatting (PHPOffice#3053)
Fix PHPOffice#1929. This was already substantially fixed, but there was a lingering problem with an unexpected leading space. It turns out there was also a problem with leading zeros, also fixed. There are also problems involving commas; fixing those seems too complicated to delay these changes, but I will add it to my to-do list.
1 parent 2fe66d0 commit 3e8d505

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/PhpSpreadsheet/Style/NumberFormat/PercentageFormatter.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public static function format($value, string $format): string
2020

2121
$format = str_replace('%', '%%', $format);
2222
$wholePartSize = strlen((string) floor($value));
23-
$decimalPartSize = $placeHolders = 0;
23+
$decimalPartSize = 0;
24+
$placeHolders = '';
2425
// Number of decimals
2526
if (preg_match('/\.([?0]+)/u', $format, $matches)) {
2627
$decimalPartSize = strlen($matches[1]);
@@ -29,12 +30,13 @@ public static function format($value, string $format): string
2930
$placeHolders = str_repeat(' ', strlen($matches[1]) - $decimalPartSize);
3031
}
3132
// Number of digits to display before the decimal
32-
if (preg_match('/([#0,]+)\./u', $format, $matches)) {
33-
$wholePartSize = max($wholePartSize, strlen($matches[1]));
33+
if (preg_match('/([#0,]+)\.?/u', $format, $matches)) {
34+
$firstZero = preg_replace('/^[#,]*/', '', $matches[1]);
35+
$wholePartSize = max($wholePartSize, strlen($firstZero));
3436
}
3537

36-
$wholePartSize += $decimalPartSize;
37-
$replacement = "{$wholePartSize}.{$decimalPartSize}";
38+
$wholePartSize += $decimalPartSize + (int) ($decimalPartSize > 0);
39+
$replacement = "0{$wholePartSize}.{$decimalPartSize}";
3840
$mask = (string) preg_replace('/[#0,]+\.?[?#0,]*/ui', "%{$replacement}f{$placeHolders}", $format);
3941

4042
/** @var float */

tests/PhpSpreadsheetTests/Style/NumberFormatTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected function tearDown(): void
4747
public function testFormatValueWithMask($expectedResult, ...$args): void
4848
{
4949
$result = NumberFormat::toFormattedString(...$args);
50-
self::assertEquals($expectedResult, $result);
50+
self::assertSame($expectedResult, $result);
5151
}
5252

5353
public function providerNumberFormat(): array

tests/data/Style/NumberFormat.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@
146146
'#,###',
147147
],
148148
[
149-
12,
149+
'12',
150150
12000,
151151
'#,',
152152
],
153153
// Scaling test
154154
[
155-
12.199999999999999,
155+
'12.2',
156156
12200000,
157157
'0.0,,',
158158
],
@@ -1486,4 +1486,9 @@
14861486
'-1111.119',
14871487
NumberFormat::FORMAT_ACCOUNTING_EUR,
14881488
],
1489+
'issue 1929' => ['(79.3%)', -0.793, '#,##0.0%;(#,##0.0%)'],
1490+
'percent without leading 0' => ['6.2%', 0.062, '##.0%'],
1491+
'percent with leading 0' => ['06.2%', 0.062, '00.0%'],
1492+
'percent lead0 no decimal' => ['06%', 0.062, '00%'],
1493+
'percent nolead0 no decimal' => ['6%', 0.062, '##%'],
14891494
];

0 commit comments

Comments
 (0)