-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify XLSX RW to keep decimal for floats with a zero decimal part
Prior to 1.10, all numeric values where read as floats. In 1.10 numeric values are read using 0 + x, which relies on PHP type juggling rules. As a result, float(0.0) is written as string('0'), then read back as int(0). This fix causes the writer to retain the the decimal for float values such that a reader can differentiate floats from ints. Closes #1262
- Loading branch information
Showing
4 changed files
with
62 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
tests/PhpSpreadsheetTests/Writer/Xlsx/FloatsRetainedTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx; | ||
|
||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as Reader; | ||
use PhpOffice\PhpSpreadsheet\Settings; | ||
use PhpOffice\PhpSpreadsheet\Shared\File; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FloatsRetainedTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider providerIntyFloatsRetainedByWriter | ||
* | ||
* @param float|int $value | ||
*/ | ||
public function testIntyFloatsRetainedByWriter($value) | ||
{ | ||
$outputFilename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); | ||
Settings::setLibXmlLoaderOptions(null); | ||
$sheet = new Spreadsheet(); | ||
$sheet->getActiveSheet()->getCell('A1')->setValue($value); | ||
|
||
$writer = new Writer($sheet); | ||
$writer->save($outputFilename); | ||
|
||
$reader = new Reader(); | ||
$sheet = $reader->load($outputFilename); | ||
|
||
$this->assertSame($value, $sheet->getActiveSheet()->getCell('A1')->getValue()); | ||
} | ||
|
||
public function providerIntyFloatsRetainedByWriter() | ||
{ | ||
return [ | ||
[-1.0], | ||
[-1], | ||
[0.0], | ||
[0], | ||
[1.0], | ||
[1], | ||
[1e-3], | ||
[1.3e-10], | ||
[1e10], | ||
[3.00000000000000000001], | ||
[99999999999999999], | ||
[99999999999999999.0], | ||
[999999999999999999999999999999999999999999], | ||
[999999999999999999999999999999999999999999.0], | ||
]; | ||
} | ||
} |