Skip to content

Commit 6ae454f

Browse files
committed
fix(Helpers): prevent trailing comma in format_number() with zero precision
1 parent 28da6fe commit 6ae454f

3 files changed

Lines changed: 7 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
@@ -31,6 +31,7 @@ Release Date: Unreleased
3131
- **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.
3232
- **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.
3333
- **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`.
34+
- **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator.
3435
- **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.
3536
- **Helpers:** Fixed escaping of attribute values in HTML media helpers to prevent malformed HTML and attribute injection.
3637
- **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 = '#,##0' . ($precision > 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public function testRomanNumberRange(): void
5353
public function testFormatNumber(): void
5454
{
5555
$this->assertSame('123,456', format_number(123456, 0, 'en_US'));
56+
$this->assertSame('123.456', format_number(123456, 0, 'de_DE'));
57+
$this->assertSame('123.456', format_number(123456, 0, 'id_ID'));
5658
}
5759

5860
public function testFormatNumberWithPrecision(): void
@@ -83,6 +85,7 @@ public function testNumberToSizeString(): void
8385
public function testKbFormat(): void
8486
{
8587
$this->assertSame('4.5 KB', number_to_size(4567, 1, 'en_US'));
88+
$this->assertSame('1 KB', number_to_size(1024, 0, 'de_DE'));
8689
}
8790

8891
public function testKbFormatMedium(): void
@@ -122,6 +125,7 @@ public function testThousands(): void
122125
public function testThousandsInt(): void
123126
{
124127
$this->assertSame('123 thousand', number_to_amount(123000, 0, 'en_US'));
128+
$this->assertSame('123 thousand', number_to_amount(123000, 0, 'de_DE'));
125129
}
126130

127131
public function testMillions(): void

0 commit comments

Comments
 (0)