diff --git a/system/Helpers/number_helper.php b/system/Helpers/number_helper.php index 5f0e59232fb3..07d3be942cf5 100644 --- a/system/Helpers/number_helper.php +++ b/system/Helpers/number_helper.php @@ -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 */ @@ -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); } diff --git a/tests/system/Helpers/NumberHelperTest.php b/tests/system/Helpers/NumberHelperTest.php index 84f05607d4a3..b9faed213888 100755 --- a/tests/system/Helpers/NumberHelperTest.php +++ b/tests/system/Helpers/NumberHelperTest.php @@ -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() diff --git a/user_guide_src/source/changelogs/v4.3.7.rst b/user_guide_src/source/changelogs/v4.3.7.rst index 01ffae52ab5d..2b180001db66 100644 --- a/user_guide_src/source/changelogs/v4.3.7.rst +++ b/user_guide_src/source/changelogs/v4.3.7.rst @@ -26,6 +26,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 ************