Skip to content

Commit c1d141b

Browse files
committed
Fix issue with prepending zero in percentage
Before it was a bug when flooring negative numbers added to the length of the number for the sprintf mask. For example -0.091 becomes -9.1 in percentage, floors to -10 which is going to increase $wholePartSize by 1 and add a leading zero to the result: -09.1% with format 0.0%. This happened for negative ranges where floor will increse the length of the original number.
1 parent dc84a51 commit c1d141b

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com)
66
and this project adheres to [Semantic Versioning](https://semver.org).
77

8-
## 2.0.0 - 2024-01-24
8+
## Unreleased - TBD
99

1010
### Added
1111

@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2929
- Changes to floating point in Php8.4. [Issue #3896](https://github.com/PHPOffice/PhpSpreadsheet/issues/3896) [PR #3897](https://github.com/PHPOffice/PhpSpreadsheet/pull/3897)
3030
- Handling User-supplied Decimal and Thousands Separators. [Issue #3900](https://github.com/PHPOffice/PhpSpreadsheet/issues/3900) [PR #3903](https://github.com/PHPOffice/PhpSpreadsheet/pull/3903)
3131
- Improve Performance of CSV Writer. [Issue #3904](https://github.com/PHPOffice/PhpSpreadsheet/issues/3904) [PR #3906](https://github.com/PHPOffice/PhpSpreadsheet/pull/3906)
32+
- Fix issue with prepending zero in percentage [Issue #3920](https://github.com/PHPOffice/PhpSpreadsheet/issues/3920) [PR #3921](https://github.com/PHPOffice/PhpSpreadsheet/pull/3921)
3233

3334
## 2.0.0 - 2024-01-04
3435

src/PhpSpreadsheet/Style/NumberFormat/PercentageFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function format($value, string $format): string
2020
$vDecimalCount = strlen(rtrim($vDecimals, '0'));
2121

2222
$format = str_replace('%', '%%', $format);
23-
$wholePartSize = strlen((string) floor($value));
23+
$wholePartSize = strlen((string) floor(abs($value)));
2424
$decimalPartSize = 0;
2525
$placeHolders = '';
2626
// Number of decimals

tests/data/Style/NumberFormat.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@
199199
0.123,
200200
'0%',
201201
],
202+
[
203+
'-9.1%',
204+
-0.091,
205+
'0.0%',
206+
],
207+
[
208+
'-99.1%',
209+
-0.991,
210+
'0.0%',
211+
],
212+
[
213+
'-9.10%',
214+
-0.091,
215+
'0.00%',
216+
],
217+
[
218+
'-99.10%',
219+
-0.991,
220+
'0.00%',
221+
],
202222
[
203223
'10%',
204224
0.1,

0 commit comments

Comments
 (0)