Skip to content

Commit c9c82b2

Browse files
authored
Merge pull request #7701 from sba/number_to_amount
Fix number comparison in number_to_amount() in number_helper.php
2 parents b84bd14 + 60b76e8 commit c9c82b2

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

system/Helpers/number_helper.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ function number_to_size($num, int $precision = 1, ?string $locale = null)
6969
*
7070
* @see https://simple.wikipedia.org/wiki/Names_for_large_numbers
7171
*
72-
* @param int|string $num
72+
* @param int|string $num Will be cast as int
73+
* @param int $precision [optional] The optional number of decimal digits to round to.
74+
* @param string|null $locale [optional]
7375
*
7476
* @return bool|string
7577
*/
@@ -91,19 +93,19 @@ function number_to_amount($num, int $precision = 0, ?string $locale = null)
9193
$generalLocale = substr($locale, 0, $underscorePos);
9294
}
9395

94-
if ($num > 1_000_000_000_000_000) {
96+
if ($num >= 1_000_000_000_000_000) {
9597
$suffix = lang('Number.quadrillion', [], $generalLocale);
9698
$num = round(($num / 1_000_000_000_000_000), $precision);
97-
} elseif ($num > 1_000_000_000_000) {
99+
} elseif ($num >= 1_000_000_000_000) {
98100
$suffix = lang('Number.trillion', [], $generalLocale);
99101
$num = round(($num / 1_000_000_000_000), $precision);
100-
} elseif ($num > 1_000_000_000) {
102+
} elseif ($num >= 1_000_000_000) {
101103
$suffix = lang('Number.billion', [], $generalLocale);
102104
$num = round(($num / 1_000_000_000), $precision);
103-
} elseif ($num > 1_000_000) {
105+
} elseif ($num >= 1_000_000) {
104106
$suffix = lang('Number.million', [], $generalLocale);
105107
$num = round(($num / 1_000_000), $precision);
106-
} elseif ($num > 1000) {
108+
} elseif ($num >= 1000) {
107109
$suffix = lang('Number.thousand', [], $generalLocale);
108110
$num = round(($num / 1000), $precision);
109111
}

tests/system/Helpers/NumberHelperTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,45 @@ public function testTbFormat()
101101
public function testThousands()
102102
{
103103
$this->assertSame('123 thousand', number_to_amount('123,000', 0, 'en_US'));
104+
$this->assertSame('1 thousand', number_to_amount('1000', 0, 'en_US'));
105+
$this->assertSame('999 thousand', number_to_amount('999499', 0, 'en_US'));
106+
$this->assertSame('1,000 thousand', number_to_amount('999500', 0, 'en_US'));
107+
$this->assertSame('1,000 thousand', number_to_amount('999999', 0, 'en_US'));
104108
}
105109

106110
public function testMillions()
107111
{
108112
$this->assertSame('123.4 million', number_to_amount('123,400,000', 1, 'en_US'));
113+
$this->assertSame('1 million', number_to_amount('1,000,000', 1, 'en_US'));
114+
$this->assertSame('1.5 million', number_to_amount('1,499,999', 1, 'en_US'));
115+
$this->assertSame('1.5 million', number_to_amount('1,500,000', 1, 'en_US'));
116+
$this->assertSame('1.5 million', number_to_amount('1,549,999', 1, 'en_US'));
117+
$this->assertSame('1.6 million', number_to_amount('1,550,000', 1, 'en_US'));
118+
$this->assertSame('999.5 million', number_to_amount('999,500,000', 1, 'en_US'));
119+
$this->assertSame('1,000 million', number_to_amount('999,500,000', 0, 'en_US'));
120+
$this->assertSame('1,000 million', number_to_amount('999,999,999', 1, 'en_US'));
109121
}
110122

111123
public function testBillions()
112124
{
113125
$this->assertSame('123.46 billion', number_to_amount('123,456,000,000', 2, 'en_US'));
126+
$this->assertSame('1 billion', number_to_amount('1,000,000,000', 2, 'en_US'));
127+
$this->assertSame('1,000 billion', number_to_amount('999,999,999,999', 2, 'en_US'));
114128
}
115129

116130
public function testTrillions()
117131
{
118132
$this->assertSame('123.457 trillion', number_to_amount('123,456,700,000,000', 3, 'en_US'));
133+
$this->assertSame('1 trillion', number_to_amount('1,000,000,000,000', 3, 'en_US'));
134+
$this->assertSame('1,000 trillion', number_to_amount('999,999,999,999,999', 3, 'en_US'));
119135
}
120136

121137
public function testQuadrillions()
122138
{
123139
$this->assertSame('123.5 quadrillion', number_to_amount('123,456,700,000,000,000', 1, 'en_US'));
140+
$this->assertSame('1 quadrillion', number_to_amount('1,000,000,000,000,000', 0, 'en_US'));
141+
$this->assertSame('1,000 quadrillion', number_to_amount('999,999,999,999,999,999', 0, 'en_US'));
142+
$this->assertSame('1,000 quadrillion', number_to_amount('1,000,000,000,000,000,000', 0, 'en_US'));
124143
}
125144

126145
public function testCurrencyCurrentLocale()

user_guide_src/source/changelogs/v4.3.7.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Message Changes
2626
Changes
2727
*******
2828

29+
- The number helper function :php:func:`number_to_amount()`, which previously
30+
returned "1000", has been corrected to return "1 thousand" when the number
31+
is exactly 1000, for example.
32+
2933
Deprecations
3034
************
3135

0 commit comments

Comments
 (0)