From 6ae454fbd1541b836de24936befd38c994ed6475 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:49:23 +0700 Subject: [PATCH 1/2] fix(Helpers): prevent trailing comma in format_number() with zero precision --- docs/changelogs/v4.7.7.md | 1 + system/Helpers/number_helper.php | 5 ++--- tests/system/Helpers/NumberHelperTest.php | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 40692b65213d..80fdd3ed9651 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -31,6 +31,7 @@ Release Date: Unreleased - **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas. - **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters. - **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`. +- **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator. - **Helpers:** Fixed bugs in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination, non-array intermediate values threw a `TypeError`, and paths with segments after a wildcard failed when the wildcard matched multiple elements. - **Helpers:** Fixed escaping of attribute values in HTML media helpers to prevent malformed HTML and attribute injection. - **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access. diff --git a/system/Helpers/number_helper.php b/system/Helpers/number_helper.php index 2f4f6f9bed74..e3ef1ec0a565 100644 --- a/system/Helpers/number_helper.php +++ b/system/Helpers/number_helper.php @@ -151,14 +151,13 @@ function format_number(float $num, int $precision = 1, ?string $locale = null, a } else { // In order to specify a precision, we'll have to modify // the pattern used by NumberFormatter. - $pattern = '#,##0.' . str_repeat('#', $precision); + $pattern = '#,##0' . ($precision > 0 ? '.' . str_repeat('#', $precision) : ''); $formatter->setPattern($pattern); $output = $formatter->format($num); } - // This might lead a trailing period if $precision == 0 - $output = trim($output, '. '); + $output = trim($output); if (intl_is_failure($formatter->getErrorCode())) { throw new BadFunctionCallException($formatter->getErrorMessage()); diff --git a/tests/system/Helpers/NumberHelperTest.php b/tests/system/Helpers/NumberHelperTest.php index 7e5fe2a358c4..930753d04f00 100644 --- a/tests/system/Helpers/NumberHelperTest.php +++ b/tests/system/Helpers/NumberHelperTest.php @@ -53,6 +53,8 @@ public function testRomanNumberRange(): void public function testFormatNumber(): void { $this->assertSame('123,456', format_number(123456, 0, 'en_US')); + $this->assertSame('123.456', format_number(123456, 0, 'de_DE')); + $this->assertSame('123.456', format_number(123456, 0, 'id_ID')); } public function testFormatNumberWithPrecision(): void @@ -83,6 +85,7 @@ public function testNumberToSizeString(): void public function testKbFormat(): void { $this->assertSame('4.5 KB', number_to_size(4567, 1, 'en_US')); + $this->assertSame('1 KB', number_to_size(1024, 0, 'de_DE')); } public function testKbFormatMedium(): void @@ -122,6 +125,7 @@ public function testThousands(): void public function testThousandsInt(): void { $this->assertSame('123 thousand', number_to_amount(123000, 0, 'en_US')); + $this->assertSame('123 thousand', number_to_amount(123000, 0, 'de_DE')); } public function testMillions(): void From 71bea6435222c05059e5c307698556abde71edc6 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:22:56 +0700 Subject: [PATCH 2/2] fix(Helpers): preserve ValueError on negative precision in format_number() --- system/Helpers/number_helper.php | 2 +- tests/system/Helpers/NumberHelperTest.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/system/Helpers/number_helper.php b/system/Helpers/number_helper.php index e3ef1ec0a565..4d62cab70ffb 100644 --- a/system/Helpers/number_helper.php +++ b/system/Helpers/number_helper.php @@ -151,7 +151,7 @@ function format_number(float $num, int $precision = 1, ?string $locale = null, a } else { // In order to specify a precision, we'll have to modify // the pattern used by NumberFormatter. - $pattern = '#,##0' . ($precision > 0 ? '.' . str_repeat('#', $precision) : ''); + $pattern = $precision === 0 ? '#,##0' : '#,##0.' . str_repeat('#', $precision); $formatter->setPattern($pattern); $output = $formatter->format($num); diff --git a/tests/system/Helpers/NumberHelperTest.php b/tests/system/Helpers/NumberHelperTest.php index 930753d04f00..22409904988c 100644 --- a/tests/system/Helpers/NumberHelperTest.php +++ b/tests/system/Helpers/NumberHelperTest.php @@ -15,6 +15,7 @@ use CodeIgniter\Test\CIUnitTestCase; use PHPUnit\Framework\Attributes\Group; +use ValueError; /** * @internal @@ -63,6 +64,14 @@ public function testFormatNumberWithPrecision(): void $this->assertSame('123,456.79', format_number(123456.789, 2, 'en_US')); } + public function testFormatNumberWithNegativePrecisionThrowsValueError(): void + { + $this->expectException(ValueError::class); + $this->expectExceptionMessage('str_repeat(): Argument #2 ($times) must be greater than or equal to 0'); + + format_number(123456, -1, 'en_US'); + } + public function testFormattingOptions(): void { $options = [