Skip to content

Additional Floating Point Precision Changes #4575

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

Merged
merged 3 commits into from
Aug 8, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a

### Fixed

- Additional floating-point precision changes. [Issue #1324](https://github.com/PHPOffice/PhpSpreadsheet/issues/1324) [PR #4575](https://github.com/PHPOffice/PhpSpreadsheet/pull/4575)
- Header/Footer images expand location. [Issue #484](https://github.com/PHPOffice/PhpSpreadsheet/issues/484) [Issue #1318](https://github.com/PHPOffice/PhpSpreadsheet/issues/1318) [PR #4572](https://github.com/PHPOffice/PhpSpreadsheet/pull/4572)
- Create uninitialized cell if used in calculation. [Issue #4558](https://github.com/PHPOffice/PhpSpreadsheet/issues/4558) [Issue #4530](https://github.com/PHPOffice/PhpSpreadsheet/issues/4530) [PR #4565](https://github.com/PHPOffice/PhpSpreadsheet/pull/4565)
- Shared/Date::isDateTime handle cells which calculate as arrays. [Issue #4557](https://github.com/PHPOffice/PhpSpreadsheet/issues/4557) [PR #4562](https://github.com/PHPOffice/PhpSpreadsheet/pull/4562)
Expand Down
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ public static function toFormattedString($value, string $format, null|array|call
// For 'General' format code, we just pass the value although this is not entirely the way Excel does it,
// it seems to round numbers to a total of 10 digits.
if (($format === NumberFormat::FORMAT_GENERAL) || ($format === NumberFormat::FORMAT_TEXT)) {
return self::adjustSeparators((string) $value);
return self::adjustSeparators(
StringHelper::convertToString($value)
);
}

// Ignore square-$-brackets prefix in format string, like "[$-411]ge.m.d", "[$-010419]0%", etc
Expand Down
37 changes: 19 additions & 18 deletions src/PhpSpreadsheet/Writer/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace PhpOffice\PhpSpreadsheet\Writer;

use Composer\Pcre\Preg;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Stringable;

class Csv extends BaseWriter
{
Expand Down Expand Up @@ -133,6 +133,7 @@ public function save($filename, int $flags = 0): void
}
}
}
/** @var string[] $cellsArray */
$this->writeLine($this->fileHandle, $cellsArray);
}

Expand Down Expand Up @@ -271,25 +272,11 @@ public function getEnclosureRequired(): bool
return $this->enclosureRequired;
}

/**
* Convert boolean to TRUE/FALSE; otherwise return element cast to string.
*
* @param null|bool|float|int|string|Stringable $element element to be converted
*/
private static function elementToString(mixed $element): string
{
if (is_bool($element)) {
return $element ? 'TRUE' : 'FALSE';
}

return (string) $element;
}

/**
* Write line to CSV file.
*
* @param resource $fileHandle PHP filehandle
* @param mixed[] $values Array containing values in a row
* @param string[] $values Array containing values in a row
*/
private function writeLine($fileHandle, array $values): void
{
Expand All @@ -299,9 +286,23 @@ private function writeLine($fileHandle, array $values): void
// Build the line
$line = '';

/** @var null|bool|float|int|string|Stringable $element */
foreach ($values as $element) {
$element = self::elementToString($element);
if (Preg::isMatch('/^([+-])?(\d+)[.](\d+)/', $element, $matches)) {
// Excel will "convert" file with pop-up
// if there are more than 15 digits precision.
$whole = $matches[2];
if ($whole !== '0') {
$wholeLen = strlen($whole);
$frac = $matches[3];
$maxFracLen = 15 - $wholeLen;
if ($maxFracLen >= 0 && strlen($frac) > $maxFracLen) {
$result = sprintf("%.{$maxFracLen}F", $element);
if (str_contains($result, '.')) {
$element = Preg::replace('/[.]?0+$/', '', $result); // strip trailing zeros
}
}
}
}
// Add delimiter
$line .= $delimiter;
$delimiter = $this->delimiter;
Expand Down
48 changes: 48 additions & 0 deletions tests/PhpSpreadsheetTests/Shared/Issue1324Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Shared;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Csv as CsvWriter;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class Issue1324Test extends AbstractFunctional
{
protected static int $version = 80400;

public function testPrecision(): void
{
$string1 = '753.149999999999';
$s1 = (float) $string1;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue($s1);
self::assertNotSame(753.15, $sheet->getCell('A1')->getValue());
$formats = ['Csv', 'Xlsx', 'Xls', 'Ods', 'Html'];
foreach ($formats as $format) {
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
$rsheet = $reloadedSpreadsheet->getActiveSheet();
$s2 = $rsheet->getCell('A1')->getValue();
self::assertSame($s1, $s2, "difference for $format");
$reloadedSpreadsheet->disconnectWorksheets();
}
$spreadsheet->disconnectWorksheets();
}

public function testCsv(): void
{
$string1 = '753.149999999999';
$s1 = (float) $string1;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue($s1);
$writer = new CsvWriter($spreadsheet);
ob_start();
$writer->save('php://output');
$output = (string) ob_get_clean();
self::assertStringContainsString($string1, $output);
$spreadsheet->disconnectWorksheets();
}
}