Skip to content

Commit

Permalink
Fix issue with prepending zero in percentage
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nicholasruunu committed Feb 27, 2024
1 parent dc84a51 commit aa640bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com)
and this project adheres to [Semantic Versioning](https://semver.org).

## 2.0.0 - 2024-01-24
## 2.0.0 - 2024-02-27

### Added

Expand All @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- 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)
- 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)
- Improve Performance of CSV Writer. [Issue #3904](https://github.com/PHPOffice/PhpSpreadsheet/issues/3904) [PR #3906](https://github.com/PHPOffice/PhpSpreadsheet/pull/3906)
- Fix issue with prepending zero in percentage -1.0<n<-0.9 [Issue #3920](https://github.com/PHPOffice/PhpSpreadsheet/issues/3920) [PR #3921](https://github.com/PHPOffice/PhpSpreadsheet/pull/3921)

## 2.0.0 - 2024-01-04

Expand Down
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Style/NumberFormat/PercentageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class PercentageFormatter extends BaseFormatter
/** @param float|int $value */
public static function format($value, string $format): string
{
if ($format === NumberFormat::FORMAT_PERCENTAGE) {
return round((100 * $value), 0) . '%';
if (NumberFormat::FORMAT_PERCENTAGE === $format) {
return round((100 * $value)) . '%';
}

$value *= 100;
Expand All @@ -20,7 +20,7 @@ public static function format($value, string $format): string
$vDecimalCount = strlen(rtrim($vDecimals, '0'));

$format = str_replace('%', '%%', $format);
$wholePartSize = strlen((string) floor($value));
$wholePartSize = strlen((string) floor(abs($value)));
$decimalPartSize = 0;
$placeHolders = '';
// Number of decimals
Expand Down
20 changes: 20 additions & 0 deletions tests/data/Style/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@
0.123,
'0%',
],
[
'-9.1%',
-0.091,
'0.0%',
],
[
'-99.1%',
-0.991,
'0.0%',
],
[
'-9.10%',
-0.091,
'0.00%',
],
[
'-99.10%',
-0.991,
'0.00%',
],
[
'10%',
0.1,
Expand Down

0 comments on commit aa640bb

Please sign in to comment.