mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-10-17 17:40:17 +00:00
f553019f95
Fix #4113. TRUNC isn't always producing the expected result. There was a promising algorithm at https://stackoverflow.com/questions/4668628/truncate-float-numbers-with-php from user Juan. It works through Php8.3, but failed in Php8.4 (more on this later). User Savageman on the same page has a solution that needs work, but, once the work had taken place, it works on Php8.1-8.4. The ROUNDUP and ROUNDDOWN functions were adversely affected by Php8.4, probably for the same reasons as Juan's TRUNC suggestion. I put a kludge in place for them some time ago, but I wasn't happy with it. The solution used for TRUNC here suggested a change to the ROUNDUP and ROUNDDOWN code that would no longer require the kludge. The change to those functions now works more cleanly on Php8.1-8.4.
45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
return [
|
|
[3.14, '3.14159, 2'],
|
|
[3.141, '3.14159, 3'],
|
|
[-3.14, '-3.14159, 2'],
|
|
[-3.141, '-3.14159, 3'],
|
|
[31415.92654, '31415.92654, 10'],
|
|
[-31415.92654, '-31415.92654, 10'],
|
|
[31415.92, '31415.92654, 2'],
|
|
[31400, '31415.92654, -2'],
|
|
'negative number and precision' => [-31400, '-31415.92654, -2'],
|
|
[0, '31415.92654, -10'],
|
|
[0, '-31415.92654, -10'],
|
|
[12000, '12345.6789, -3'],
|
|
[12300, '12345.6789, -2'],
|
|
[12340, '12345.6789, -1'],
|
|
[12345, '12345.6789, 0'],
|
|
[12345.6, '12345.6789, 1'],
|
|
[12345.67, '12345.6789, 2'],
|
|
[12345.678, '12345.6789, 3'],
|
|
['#VALUE!', '"ABC", 2'],
|
|
['#VALUE!', '31415.92654, "ABC"'],
|
|
['#VALUE!', '""'],
|
|
[1, 'true'],
|
|
[0, 'false'],
|
|
[0, '0'],
|
|
['exception', ''],
|
|
[1, 'A2'],
|
|
[2, 'A3'],
|
|
[-3, 'A4'],
|
|
[-5, 'A5'],
|
|
[0, 'B1'],
|
|
'issue4113' => [1.0, '1.01, 1'],
|
|
'issue4113 negative' => [-1.0, '-1.01, 1'],
|
|
'issue4113 additional' => [10.04, '10.04, 2'],
|
|
'issue4113 additional negative' => [-10.04, '-10.04, 2'],
|
|
'issue4113 small fraction keep all' => [0.04, '0.04, 2'],
|
|
'issue4113 small negative fraction keep all' => [-0.04, '-0.04, 2'],
|
|
'issue4113 small fraction lose some' => [0.0, '0.01, 1'],
|
|
'issue4113 small negative fraction lose some' => [0.0, '-0.001, 1'],
|
|
];
|