Skip to content

Conversation

@oleibman
Copy link
Collaborator

@oleibman oleibman commented Sep 10, 2025

Fix #4626. Previous changes had increased the precision of floating point numbers when cast to string, making for greater accuracy after save and load operations, without affecting the values displayed by Excel. Although the results of the cast are now more accurate computationally, they can appear unexpected to humans. A new boolean parameter lessFloatPrecision (defaulting to false) is added to StringHelper::convertToString, to NumberFormat::toFormattedString and NumberFormat\Formatter::toFormattedString, and to the entire Worksheet::toArray family of functions. When the new parameter is set to true, the result can be less surprising to humans. It should not, however, be used in subsequent computations.

In the case of the NumberFormat functions, the new parameter will be considered only when the NumberFormat for the cell in question is General or equivalent. Setting an actual numeric format for the cell is probably a better solution than using the new parameter.

This is:

  • a bugfix
  • a new feature
  • refactoring
  • additional unit tests

Checklist:

  • Changes are covered by unit tests
    • Changes are covered by existing unit tests
    • New unit tests have been added
  • Code style is respected
  • Commit message explains why the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
  • CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
  • Documentation is updated as necessary

Fix PHPOffice#4626. Previous changes had increased the precision of floating point numbers when cast to string, making for greater accuracy after save and load operations, without affecting the values displayed by Excel. Although the results of the cast are now more accurate computationally, they can appear unexpected to humans. A new boolean parameter `lessFloatPrecision` (defaulting to false) is added to `StringHelper::convertToString`, to `NumberFormat::toFormattedString` and `NumberFormat\Formatter::toFormattedString`, and to the entire `Worksheet::toArray` family of functions. When the new parameter is set to true, the result can be less surprising to humans. It should not, however, be used in subsequent computations.

In the case of the NumberFormat functions, the new parameter will be considered only when the NumberFormat for the cell in question is `General` or equivalent. Setting an actual numeric format for the cell is probably a better solution than using the new parameter.
@oleibman oleibman enabled auto-merge September 13, 2025 14:56
@oleibman oleibman added this pull request to the merge queue Sep 13, 2025
Merged via the queue into PHPOffice:master with commit 996de17 Sep 13, 2025
13 checks passed
@oleibman oleibman deleted the lessprecise branch September 13, 2025 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Regression with floats in 5.0.0

1 participant