diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index fbd3edc38691..f034a66a8eec 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -46,12 +46,22 @@ public static function format(int|float $number, ?int $precision = null, ?int $m * * @param int|float $number * @param string|null $locale + * @param int|null $after + * @param int|null $until * @return string */ - public static function spell(int|float $number, ?string $locale = null) + public static function spell(int|float $number, ?string $locale = null, ?int $after = null, ?int $until = null) { static::ensureIntlExtensionIsInstalled(); + if (! is_null($after) && $number <= $after) { + return static::format($number, locale: $locale); + } + + if (! is_null($until) && $number >= $until) { + return static::format($number, locale: $locale); + } + $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT); return $formatter->format($number); diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index ef1e619081f2..c15dafddf812 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -77,6 +77,22 @@ public function testSpelloutWithLocale() $this->assertSame('trois', Number::spell(3, 'fr')); } + public function testSpelloutWithThreshold() + { + $this->needsIntlExtension(); + + $this->assertSame('9', Number::spell(9, after: 10)); + $this->assertSame('10', Number::spell(10, after: 10)); + $this->assertSame('eleven', Number::spell(11, after: 10)); + + $this->assertSame('nine', Number::spell(9, until: 10)); + $this->assertSame('10', Number::spell(10, until: 10)); + $this->assertSame('11', Number::spell(11, until: 10)); + + $this->assertSame('ten thousand', Number::spell(10000, until: 50000)); + $this->assertSame('100,000', Number::spell(100000, until: 50000)); + } + public function testOrdinal() { $this->assertSame('1st', Number::ordinal(1));