Minor Fix for Percentage Formatting (#3053)
Fix #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.
This commit is contained in:
parent
2fe66d097f
commit
3e8d50547c
|
|
@ -20,7 +20,8 @@ class PercentageFormatter extends BaseFormatter
|
|||
|
||||
$format = str_replace('%', '%%', $format);
|
||||
$wholePartSize = strlen((string) floor($value));
|
||||
$decimalPartSize = $placeHolders = 0;
|
||||
$decimalPartSize = 0;
|
||||
$placeHolders = '';
|
||||
// Number of decimals
|
||||
if (preg_match('/\.([?0]+)/u', $format, $matches)) {
|
||||
$decimalPartSize = strlen($matches[1]);
|
||||
|
|
@ -29,12 +30,13 @@ class PercentageFormatter extends BaseFormatter
|
|||
$placeHolders = str_repeat(' ', strlen($matches[1]) - $decimalPartSize);
|
||||
}
|
||||
// Number of digits to display before the decimal
|
||||
if (preg_match('/([#0,]+)\./u', $format, $matches)) {
|
||||
$wholePartSize = max($wholePartSize, strlen($matches[1]));
|
||||
if (preg_match('/([#0,]+)\.?/u', $format, $matches)) {
|
||||
$firstZero = preg_replace('/^[#,]*/', '', $matches[1]);
|
||||
$wholePartSize = max($wholePartSize, strlen($firstZero));
|
||||
}
|
||||
|
||||
$wholePartSize += $decimalPartSize;
|
||||
$replacement = "{$wholePartSize}.{$decimalPartSize}";
|
||||
$wholePartSize += $decimalPartSize + (int) ($decimalPartSize > 0);
|
||||
$replacement = "0{$wholePartSize}.{$decimalPartSize}";
|
||||
$mask = (string) preg_replace('/[#0,]+\.?[?#0,]*/ui', "%{$replacement}f{$placeHolders}", $format);
|
||||
|
||||
/** @var float */
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ class NumberFormatTest extends TestCase
|
|||
public function testFormatValueWithMask($expectedResult, ...$args): void
|
||||
{
|
||||
$result = NumberFormat::toFormattedString(...$args);
|
||||
self::assertEquals($expectedResult, $result);
|
||||
self::assertSame($expectedResult, $result);
|
||||
}
|
||||
|
||||
public function providerNumberFormat(): array
|
||||
|
|
|
|||
|
|
@ -146,13 +146,13 @@ return [
|
|||
'#,###',
|
||||
],
|
||||
[
|
||||
12,
|
||||
'12',
|
||||
12000,
|
||||
'#,',
|
||||
],
|
||||
// Scaling test
|
||||
[
|
||||
12.199999999999999,
|
||||
'12.2',
|
||||
12200000,
|
||||
'0.0,,',
|
||||
],
|
||||
|
|
@ -1486,4 +1486,9 @@ return [
|
|||
'-1111.119',
|
||||
NumberFormat::FORMAT_ACCOUNTING_EUR,
|
||||
],
|
||||
'issue 1929' => ['(79.3%)', -0.793, '#,##0.0%;(#,##0.0%)'],
|
||||
'percent without leading 0' => ['6.2%', 0.062, '##.0%'],
|
||||
'percent with leading 0' => ['06.2%', 0.062, '00.0%'],
|
||||
'percent lead0 no decimal' => ['06%', 0.062, '00%'],
|
||||
'percent nolead0 no decimal' => ['6%', 0.062, '##%'],
|
||||
];
|
||||
|
|
|
|||
Loading…
Reference in New Issue