Skip to content

Commit

Permalink
Merge pull request #8932 from kenjis/fix-number-helper-type-error
Browse files Browse the repository at this point in the history
fix: TypeError in  number_to_amount()
  • Loading branch information
kenjis authored Jun 4, 2024
2 parents 482b8bb + 3d6436e commit 4dd9a87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion system/Helpers/number_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ function number_to_amount($num, int $precision = 0, ?string $locale = null)
// Strip any formatting & ensure numeric input
try {
// @phpstan-ignore-next-line
$num = 0 + str_replace(',', '', $num);
$num = 0 + str_replace(',', '', (string) $num);
} catch (ErrorException) {
// Catch "Warning: A non-numeric value encountered"
return false;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/system/Helpers/NumberHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function testRomanNumber(): void
$this->assertSame('X', number_to_roman(10));
}

public function testRomanNumberString(): void
{
$this->assertSame('XCVI', number_to_roman('96'));
}

public function testRomanNumberRange(): void
{
$this->assertNull(number_to_roman(-1));
Expand Down Expand Up @@ -70,6 +75,11 @@ public function testNumberToSize(): void
$this->assertSame('456 Bytes', number_to_size(456, 1, 'en_US'));
}

public function testNumberToSizeString(): void
{
$this->assertSame('456 Bytes', number_to_size('456', 1, 'en_US'));
}

public function testKbFormat(): void
{
$this->assertSame('4.5 KB', number_to_size(4567, 1, 'en_US'));
Expand Down Expand Up @@ -109,6 +119,11 @@ public function testThousands(): void
$this->assertSame('1,000 thousand', number_to_amount('999999', 0, 'en_US'));
}

public function testThousandsInt(): void
{
$this->assertSame('123 thousand', number_to_amount(123000, 0, 'en_US'));
}

public function testMillions(): void
{
$this->assertSame('123.4 million', number_to_amount('123,400,000', 1, 'en_US'));
Expand Down

0 comments on commit 4dd9a87

Please sign in to comment.