Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify XLSX RW to keep decimal for floats with a zero decimal part #1262

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,15 +727,6 @@ public function load($pFilename)

// read empty cells or the cells are not empty
if ($this->readEmptyCells || ($value !== null && $value !== '')) {
// Check for numeric values
if (is_numeric($value) && $cellDataType != 's') {
if ($value == (int) $value) {
$value = (int) $value;
} elseif ($value == (float) $value) {
$value = (float) $value;
}
}

// Rich text?
if ($value instanceof RichText && $this->readDataOnly) {
$value = $value->getPlainText();
Expand Down
7 changes: 7 additions & 0 deletions src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,13 @@ private function writeCell(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet

break;
case 'n': // Numeric
//force a decimal to be written if the type is float
if (is_float($cellValue)) {
$cellValue = (string) $cellValue;
if (strpos($cellValue, '.') === false) {
$cellValue = $cellValue . '.0';
}
}
// force point as decimal separator in case current locale uses comma
$objWriter->writeElement('v', str_replace(',', '.', $cellValue));

Expand Down
54 changes: 54 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/FloatsRetainedTest.php
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],
];
}
}