Skip to content

Commit e4d791d

Browse files
authored
fix: prevent trailing comma in format_number() with zero precision (#10793)
1 parent 0699287 commit e4d791d

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Release Date: Unreleased
3535
- **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.
3636
- **Debug Toolbar:** Fixed a bug where `History::setFiles()` emitted a PHP warning when attempting to delete a debugbar history file that had already been removed.
3737
- **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`.
38+
- **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator.
3839
- **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.
3940
- **Helpers:** Fixed escaping of attribute values in HTML media helpers to prevent malformed HTML and attribute injection.
4041
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.

‎system/Helpers/number_helper.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,13 @@ function format_number(float $num, int $precision = 1, ?string $locale = null, a
151151
} else {
152152
// In order to specify a precision, we'll have to modify
153153
// the pattern used by NumberFormatter.
154-
$pattern = '#,##0.' . str_repeat('#', $precision);
154+
$pattern = $precision === 0 ? '#,##0' : '#,##0.' . str_repeat('#', $precision);
155155

156156
$formatter->setPattern($pattern);
157157
$output = $formatter->format($num);
158158
}
159159

160-
// This might lead a trailing period if $precision == 0
161-
$output = trim($output, '. ');
160+
$output = trim($output);
162161

163162
if (intl_is_failure($formatter->getErrorCode())) {
164163
throw new BadFunctionCallException($formatter->getErrorMessage());

‎tests/system/Helpers/NumberHelperTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use PHPUnit\Framework\Attributes\Group;
18+
use ValueError;
1819

1920
/**
2021
* @internal
@@ -53,6 +54,8 @@ public function testRomanNumberRange(): void
5354
public function testFormatNumber(): void
5455
{
5556
$this->assertSame('123,456', format_number(123456, 0, 'en_US'));
57+
$this->assertSame('123.456', format_number(123456, 0, 'de_DE'));
58+
$this->assertSame('123.456', format_number(123456, 0, 'id_ID'));
5659
}
5760

5861
public function testFormatNumberWithPrecision(): void
@@ -61,6 +64,14 @@ public function testFormatNumberWithPrecision(): void
6164
$this->assertSame('123,456.79', format_number(123456.789, 2, 'en_US'));
6265
}
6366

67+
public function testFormatNumberWithNegativePrecisionThrowsValueError(): void
68+
{
69+
$this->expectException(ValueError::class);
70+
$this->expectExceptionMessage('str_repeat(): Argument #2 ($times) must be greater than or equal to 0');
71+
72+
format_number(123456, -1, 'en_US');
73+
}
74+
6475
public function testFormattingOptions(): void
6576
{
6677
$options = [
@@ -83,6 +94,7 @@ public function testNumberToSizeString(): void
8394
public function testKbFormat(): void
8495
{
8596
$this->assertSame('4.5 KB', number_to_size(4567, 1, 'en_US'));
97+
$this->assertSame('1 KB', number_to_size(1024, 0, 'de_DE'));
8698
}
8799

88100
public function testKbFormatMedium(): void
@@ -122,6 +134,7 @@ public function testThousands(): void
122134
public function testThousandsInt(): void
123135
{
124136
$this->assertSame('123 thousand', number_to_amount(123000, 0, 'en_US'));
137+
$this->assertSame('123 thousand', number_to_amount(123000, 0, 'de_DE'));
125138
}
126139

127140
public function testMillions(): void

0 commit comments

Comments
 (0)