Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TypeError in number_to_amount() #8932

Merged
merged 2 commits into from
Jun 4, 2024
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
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
Loading