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
14 changes: 8 additions & 6 deletions system/Helpers/number_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ function number_to_size($num, int $precision = 1, ?string $locale = null)
*
* @see https://simple.wikipedia.org/wiki/Names_for_large_numbers
*
* @param int|string $num
* @param int|string $num Will be cast as int
* @param int $precision [optional] The optional number of decimal digits to round to.
* @param string|null $locale [optional]
*
* @return bool|string
*/
Expand All @@ -91,19 +93,19 @@ function number_to_amount($num, int $precision = 0, ?string $locale = null)
$generalLocale = substr($locale, 0, $underscorePos);
}

if ($num > 1_000_000_000_000_000) {
if ($num >= 1_000_000_000_000_000) {
$suffix = lang('Number.quadrillion', [], $generalLocale);
$num = round(($num / 1_000_000_000_000_000), $precision);
} elseif ($num > 1_000_000_000_000) {
} elseif ($num >= 1_000_000_000_000) {
$suffix = lang('Number.trillion', [], $generalLocale);
$num = round(($num / 1_000_000_000_000), $precision);
} elseif ($num > 1_000_000_000) {
} elseif ($num >= 1_000_000_000) {
$suffix = lang('Number.billion', [], $generalLocale);
$num = round(($num / 1_000_000_000), $precision);
} elseif ($num > 1_000_000) {
} elseif ($num >= 1_000_000) {
$suffix = lang('Number.million', [], $generalLocale);
$num = round(($num / 1_000_000), $precision);
} elseif ($num > 1000) {
} elseif ($num >= 1000) {
$suffix = lang('Number.thousand', [], $generalLocale);
$num = round(($num / 1000), $precision);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/system/Helpers/NumberHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,45 @@ public function testTbFormat()
public function testThousands()
{
$this->assertSame('123 thousand', number_to_amount('123,000', 0, 'en_US'));
$this->assertSame('1 thousand', number_to_amount('1000', 0, 'en_US'));
$this->assertSame('999 thousand', number_to_amount('999499', 0, 'en_US'));
$this->assertSame('1,000 thousand', number_to_amount('999500', 0, 'en_US'));
$this->assertSame('1,000 thousand', number_to_amount('999999', 0, 'en_US'));
}

public function testMillions()
{
$this->assertSame('123.4 million', number_to_amount('123,400,000', 1, 'en_US'));
$this->assertSame('1 million', number_to_amount('1,000,000', 1, 'en_US'));
$this->assertSame('1.5 million', number_to_amount('1,499,999', 1, 'en_US'));
$this->assertSame('1.5 million', number_to_amount('1,500,000', 1, 'en_US'));
$this->assertSame('1.5 million', number_to_amount('1,549,999', 1, 'en_US'));
$this->assertSame('1.6 million', number_to_amount('1,550,000', 1, 'en_US'));
$this->assertSame('999.5 million', number_to_amount('999,500,000', 1, 'en_US'));
$this->assertSame('1,000 million', number_to_amount('999,500,000', 0, 'en_US'));
$this->assertSame('1,000 million', number_to_amount('999,999,999', 1, 'en_US'));
}

public function testBillions()
{
$this->assertSame('123.46 billion', number_to_amount('123,456,000,000', 2, 'en_US'));
$this->assertSame('1 billion', number_to_amount('1,000,000,000', 2, 'en_US'));
$this->assertSame('1,000 billion', number_to_amount('999,999,999,999', 2, 'en_US'));
}

public function testTrillions()
{
$this->assertSame('123.457 trillion', number_to_amount('123,456,700,000,000', 3, 'en_US'));
$this->assertSame('1 trillion', number_to_amount('1,000,000,000,000', 3, 'en_US'));
$this->assertSame('1,000 trillion', number_to_amount('999,999,999,999,999', 3, 'en_US'));
}

public function testQuadrillions()
{
$this->assertSame('123.5 quadrillion', number_to_amount('123,456,700,000,000,000', 1, 'en_US'));
$this->assertSame('1 quadrillion', number_to_amount('1,000,000,000,000,000', 0, 'en_US'));
$this->assertSame('1,000 quadrillion', number_to_amount('999,999,999,999,999,999', 0, 'en_US'));
$this->assertSame('1,000 quadrillion', number_to_amount('1,000,000,000,000,000,000', 0, 'en_US'));
}

public function testCurrencyCurrentLocale()
Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/changelogs/v4.3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Message Changes
Changes
*******

- The number helper function :php:func:`number_to_amount()`, which previously
returned "1000", has been corrected to return "1 thousand" when the number
is exactly 1000, for example.

Deprecations
************

Expand Down