From bcb0843336475a2ef58209e182feeb97fad39765 Mon Sep 17 00:00:00 2001 From: Man Alin Date: Sun, 29 Nov 2026 11:04:57 +0000 Subject: [PATCH] fix(number): round values read with zero decimals Number cells keep their stored fractional value, but reading through a zero-decimal column truncated the fraction and saturated very large values at the integer maximum. Rounding consistently with other decimal precision preserves normal integer output while avoiding both forms of data distortion. Assisted-by: Devin:adaptive Signed-off-by: Man Alin Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- lib/Db/RowCellNumberMapper.php | 7 ++++--- tests/unit/Db/RowCellNumberMapperTest.php | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/Db/RowCellNumberMapper.php b/lib/Db/RowCellNumberMapper.php index 788a82162e..126a728d5b 100644 --- a/lib/Db/RowCellNumberMapper.php +++ b/lib/Db/RowCellNumberMapper.php @@ -26,11 +26,12 @@ public function formatRowData(Column $column, array $row) { return null; } $decimals = $column->getNumberDecimals() ?? 0; - if ($decimals === 0) { - return (int)$value; + $rounded = round(floatval($value), $decimals); + if ($decimals === 0 && $rounded <= PHP_INT_MAX && $rounded >= PHP_INT_MIN) { + return (int)$rounded; } - return round(floatval($value), $decimals); + return $rounded; } public function applyDataToEntity(Column $column, RowCellSuper $cell, $data): void { diff --git a/tests/unit/Db/RowCellNumberMapperTest.php b/tests/unit/Db/RowCellNumberMapperTest.php index 692f0d48a6..a49f27836f 100644 --- a/tests/unit/Db/RowCellNumberMapperTest.php +++ b/tests/unit/Db/RowCellNumberMapperTest.php @@ -15,6 +15,7 @@ class RowCellNumberMapperTest extends TestCase { private RowCellNumberMapper $mapper; private Column $column; + #[\Override] public function setUp(): void { $this->mapper = new RowCellNumberMapper($this->createMock(IDBConnection::class)); // getNumberDecimals() is resolved through Entity's __call magic and cannot be mocked @@ -50,6 +51,10 @@ public function formatRowDataProvider(): array { return [ 'integer value' => [42.0, 42], 'explicit zero' => [0.0, 0], + 'fraction rounds up' => [2.7, 3], + 'fraction rounds down' => [0.4, 0], + 'negative fraction rounds away from zero' => [-2.7, -3], + 'large value does not saturate at integer maximum' => [12345678901234567890.0, 12345678901234567890.0], // A NULL cell means "no value" and must not read back as 0. 'null value' => [null, null], 'empty string' => ['', null], @@ -59,7 +64,7 @@ public function formatRowDataProvider(): array { /** * @dataProvider formatRowDataProvider */ - public function testFormatRowData(mixed $stored, ?int $expected): void { + public function testFormatRowData(mixed $stored, int|float|null $expected): void { $this->assertSame($expected, $this->mapper->formatRowData($this->column, ['value' => $stored])); } }