Skip to content

Commit 15e1451

Browse files
authored
Merge pull request #3382 from PHPOffice/Issue-3381_Scientific-NumberFormat-Decimal-Precision
Issue 3381 - Fix decimal precision for Scientific Number Format Mask
2 parents 169a87a + c0b60fc commit 15e1451

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3535
- XLSX Writer - Array Formulas do not include function prefix [Issue #3337](https://github.com/PHPOffice/PhpSpreadsheet/issues/3337) [PR #3338](https://github.com/PHPOffice/PhpSpreadsheet/pull/3338)
3636
- Permit Max Column for Row Breaks [Issue #3143](https://github.com/PHPOffice/PhpSpreadsheet/issues/3143) [PR #3345](https://github.com/PHPOffice/PhpSpreadsheet/pull/3345)
3737
- AutoSize Columns should allow for dropdown icon when AutoFilter is for a Table [Issue #3356](https://github.com/PHPOffice/PhpSpreadsheet/issues/3356) [PR #3358](https://github.com/PHPOffice/PhpSpreadsheet/pull/3358)
38-
38+
- Decimal Precision for Scientific Number Format Mask [Issue #3381](https://github.com/PHPOffice/PhpSpreadsheet/issues/3381) [PR #3382](https://github.com/PHPOffice/PhpSpreadsheet/pull/3382)
3939

4040
## 1.27.1 - 2023-02-08
4141

src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ private static function formatStraightNumericValue($value, string $format, array
164164

165165
if (preg_match('/[0#]E[+-]0/i', $format)) {
166166
// Scientific format
167-
return sprintf('%5.2E', $valueFloat);
167+
$decimals = strlen($right);
168+
$size = $decimals + 3;
169+
170+
return sprintf("%{$size}.{$decimals}E", $valueFloat);
168171
} elseif (preg_match('/0([^\d\.]+)0/', $format) || substr_count($format, '.') > 1) {
169172
if ($valueFloat == floor($valueFloat) && substr_count($format, '.') === 1) {
170173
$value *= 10 ** strlen(explode('.', $format)[1]);

tests/data/Style/NumberFormat.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,4 +1479,19 @@
14791479
'1111.119',
14801480
'[$€-en_US]_(#,##0.00_);[$€-en_US] (#,##0.00)',
14811481
],
1482+
[
1483+
'-1.2E+4',
1484+
-12345.6789,
1485+
'0.0E+00',
1486+
],
1487+
[
1488+
'-1.23E+4',
1489+
-12345.6789,
1490+
'0.00E+00',
1491+
],
1492+
[
1493+
'-1.235E+4',
1494+
-12345.6789,
1495+
'0.000E+00',
1496+
],
14821497
];

0 commit comments

Comments
 (0)