Skip to content

Commit dcc653f

Browse files
[10.x] Add a threshold parameter to the Number::spell helper (#49610)
* [10.x] Add a `threshold` parameter to the `Number::spell` helper Adds a parameter to limit how high numbers are spelled out. * Add docblock for the threshold parameter * adjust logic * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 7a94700 commit dcc653f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Illuminate/Support/Number.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,22 @@ public static function format(int|float $number, ?int $precision = null, ?int $m
4646
*
4747
* @param int|float $number
4848
* @param string|null $locale
49+
* @param int|null $after
50+
* @param int|null $until
4951
* @return string
5052
*/
51-
public static function spell(int|float $number, ?string $locale = null)
53+
public static function spell(int|float $number, ?string $locale = null, ?int $after = null, ?int $until = null)
5254
{
5355
static::ensureIntlExtensionIsInstalled();
5456

57+
if (! is_null($after) && $number <= $after) {
58+
return static::format($number, locale: $locale);
59+
}
60+
61+
if (! is_null($until) && $number >= $until) {
62+
return static::format($number, locale: $locale);
63+
}
64+
5565
$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT);
5666

5767
return $formatter->format($number);

tests/Support/SupportNumberTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ public function testSpelloutWithLocale()
7777
$this->assertSame('trois', Number::spell(3, 'fr'));
7878
}
7979

80+
public function testSpelloutWithThreshold()
81+
{
82+
$this->needsIntlExtension();
83+
84+
$this->assertSame('9', Number::spell(9, after: 10));
85+
$this->assertSame('10', Number::spell(10, after: 10));
86+
$this->assertSame('eleven', Number::spell(11, after: 10));
87+
88+
$this->assertSame('nine', Number::spell(9, until: 10));
89+
$this->assertSame('10', Number::spell(10, until: 10));
90+
$this->assertSame('11', Number::spell(11, until: 10));
91+
92+
$this->assertSame('ten thousand', Number::spell(10000, until: 50000));
93+
$this->assertSame('100,000', Number::spell(100000, until: 50000));
94+
}
95+
8096
public function testOrdinal()
8197
{
8298
$this->assertSame('1st', Number::ordinal(1));

0 commit comments

Comments
 (0)