Skip to content

Fix a TypeError on complex number format with small enough number #3129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Allow setting AutoFilter range on a single cell or row [Issue #3102](https://github.com/PHPOffice/PhpSpreadsheet/issues/3102) [PR #3111](https://github.com/PHPOffice/PhpSpreadsheet/pull/3111)
- Xlsx Reader External Data Validations Flag Missing [Issue #2677](https://github.com/PHPOffice/PhpSpreadsheet/issues/2677) [PR #3078](https://github.com/PHPOffice/PhpSpreadsheet/pull/3078)
- Reduces extra memory usage on `__destruct()` calls

- TypeError using small numbers along a complex number format [Issue #3128](https://github.com/PHPOffice/PhpSpreadsheet/issues/3128)

## 1.25.2 - 2022-09-25

Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ private static function complexNumberFormatMask($number, string $mask, bool $spl
$number = (string) abs($numberFloat);

if ($splitOnPoint && strpos($mask, '.') !== false && strpos($number, '.') !== false) {
if (strpos($number, 'E') !== false) {
$number = sprintf('%.' . PHP_FLOAT_DIG . 'F', $number);
}
$numbers = explode('.', $number);
$masks = explode('.', $mask);
if (count($masks) > 2) {
Expand Down
7 changes: 7 additions & 0 deletions tests/PhpSpreadsheetTests/Style/NumberFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ public function testCurrencyCode(): void
self::assertEquals($rslt, '$ 12,345.679');
StringHelper::setCurrencyCode($cur);
}

public function testSmallValueWithComplexFormat(): void
{
$result = NumberFormat::toFormattedString(1E-17, '0 000.0');

self::assertEquals('0 000.0', $result);
}
}