Skip to content

Commit 6de86f5

Browse files
committed
Deal With Scientific Notation
1 parent 58ff149 commit 6de86f5

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/PhpSpreadsheet/Calculation/MathTrig/Trunc.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public static function evaluate(array|float|string|null $value = 0, array|int|st
5151

5252
return ($minusSign === '') ? $result : -$result;
5353
}
54-
$resultString = (string) $value;
54+
$decimals = PHP_FLOAT_DIG - strlen((string) (int) $value);
55+
$resultString = sprintf('%.' . $decimals . 'f', $value);
5556
$regExp = '/([.]\\d{' . $digits . '})\\d+$/';
5657
$result = $minusSign . (preg_replace($regExp, '$1', $resultString) ?? $resultString);
5758

tests/data/Calculation/MathTrig/TRUNC.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@
4545
'issue4113 example 3 positive' => [43747, '43747.99122596, 0'],
4646
'issue4113 example 4' => [-9.11, '-9.1196419, 2'],
4747
'issue4113 example 5' => [-42300.65, '-42300.65099338, 3'],
48+
'issue4113 example 6 variant 1' => [0.000012, '0.0000123, 6'],
49+
'issue4113 example 6 variant 2' => [0.0000123, '0.0000123, 8'],
50+
'issue4113 example 6 variant 3' => [-0.000012, '-0.0000123, 6'],
51+
'issue4113 example 6 variant 4' => [-0.0000123, '-0.0000123, 8'],
52+
'issue4113 example 6 variant 5' => [0.000012, '1.23E-5, 6'],
53+
'issue4113 example 6 variant 6' => [-0.0000123, '-1.23E-5, 8'],
4854
];

0 commit comments

Comments
 (0)