mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-13 03:26:26 +00:00
170b058049
Fix #4522. Although this is technically a breaking change, it is expected that very few, if any, existing programs will be affected. Nevertheless, DefaultValueBinder is implicitly used by the vast majority of programs out there, and I am reluctant to install a breaking change for something so widespread. So I will save this change for the next breaking release, which should be PhpSpreadsheet 5.0.0. There is no current schedule for that release. For strings consisting entirely of digits, DefaultValueBinder treats the value as a string if greater than PHP_INT_MAX, or an int otherwise. This prevents the loss of precision in large integers. This treatment dates back to PHPExcel, and has been in place since 2014. There are several problems with this approach. Excel itself maintains [15 digits of precision](https://support.microsoft.com/en-us/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3). So, string-vs-int should be decided at 999_999_999_999_999. This is much lower than PHP_INT_MAX for 64-bit, and much higher than for 32-bit. DefaultValueBinder is changed to use that new limit. A second problem is that DefaultValueBinder is only making this adjustment for positive integers. It is changed to test absolute value. A third problem is that DefaultValueBinder is making this adjustment only for strings, so that if you pass the number in as an int, it will not be adjusted (and will lose precision). It is changed to apply the same test for int.
63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as Reader;
|
|
use PhpOffice\PhpSpreadsheet\Shared\File;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class FloatsRetainedTest extends TestCase
|
|
{
|
|
#[DataProvider('providerIntyFloatsRetainedByWriter')]
|
|
public function testIntyFloatsRetainedByWriter(float|int $value, mixed $expected = null): void
|
|
{
|
|
if ($expected === null) {
|
|
$expected = $value;
|
|
}
|
|
$outputFilename = File::temporaryFilename();
|
|
$spreadsheet = new Spreadsheet();
|
|
$spreadsheet->getActiveSheet()
|
|
->getCell('A1')->setValue($value);
|
|
|
|
$writer = new Writer($spreadsheet);
|
|
$writer->save($outputFilename);
|
|
$spreadsheet->disconnectWorksheets();
|
|
|
|
$reader = new Reader();
|
|
$spreadsheet2 = $reader->load($outputFilename);
|
|
unlink($outputFilename);
|
|
|
|
self::assertSame(
|
|
$expected,
|
|
$spreadsheet2->getActiveSheet()
|
|
->getCell('A1')->getValue()
|
|
);
|
|
$spreadsheet2->disconnectWorksheets();
|
|
}
|
|
|
|
public static function providerIntyFloatsRetainedByWriter(): array
|
|
{
|
|
return [
|
|
[-1.0],
|
|
[-1],
|
|
[0.0],
|
|
[0],
|
|
[1.0],
|
|
[1],
|
|
[1e-3],
|
|
[1.3e-10],
|
|
[1e10],
|
|
[3.00000000000000000001],
|
|
'int but too much precision for Excel' => [99_999_999_999_999_999, '99999999999999999'],
|
|
[99_999_999_999_999_999.0],
|
|
'int > PHP_INT_MAX so stored as float' => [999_999_999_999_999_999_999_999_999_999_999_999_999_999],
|
|
[999_999_999_999_999_999_999_999_999_999_999_999_999_999.0],
|
|
];
|
|
}
|
|
}
|