Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions system/Helpers/number_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $precision === 0 ? '#,##0' : '#,##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());
Expand Down
13 changes: 13 additions & 0 deletions tests/system/Helpers/NumberHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CodeIgniter\Test\CIUnitTestCase;
use PHPUnit\Framework\Attributes\Group;
use ValueError;

/**
* @internal
Expand Down Expand Up @@ -53,6 +54,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
Expand All @@ -61,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 = [
Expand All @@ -83,6 +94,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
Expand Down Expand Up @@ -122,6 +134,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
Expand Down
Loading