Skip to content

Regression with floats in 5.0.0 #4626

@IndrekHaav

Description

@IndrekHaav

After #4575, when reading data from a spreadsheet, some numeric values are read incorrectly, with a floating point rounding error.

To reproduce:

<?php

require __DIR__ . '/vendor/autoload.php';

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->setCellValue('A1', 123.4);
echo $worksheet->getCell('A1')->getFormattedValue();
?>
  • Expected output: 123.4
  • Actual output: 123.40000000000001

Looks like the issue arises in StringHelper::convertToString(), if the result of $positive - $floor cannot be accurately represented as a float:

$ php -a
Interactive shell

php > echo 123.4 - 123;
0.40000000000001

This incorrect representation of the decimal part is later appended to the original integer part, causing the error.

I tested this with PhpSpreadsheet 5.0.0 (PHP 8.2), the issue was not present in v4.4. The above code uses a dynamically created spreadsheet, so is not specific to any file format, though I originally reproduced the issue with an ODS file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions