Skip to content

Commit

Permalink
add spell and ordinal
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 17, 2023
1 parent caaf0b2 commit d9c328c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ public static function format(int|float $number, ?int $precision = null, ?int $m
return $formatter->format($number);
}

/**
* Spell out the given number in the given locale.
*
* @param int|float $number
* @param ?string $locale
* @return string
*/
public static function spell(int|float $number, ?string $locale = null)
{
static::ensureIntlExtensionIsInstalled();

$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT);

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

/**
* Convert the given number to ordinal form.
*
* @param int|float $number
* @param ?string $locale
* @return string
*/
public static function ordinal(int|float $number, ?string $locale = null)
{
static::ensureIntlExtensionIsInstalled();

$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::ORDINAL);

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

/**
* Convert the given number to its percentage equivalent.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ public function testFormatWithAppLocale()
Number::useLocale('en');
}

public function testSpellout()
{
$this->assertSame('ten', Number::spell(10));
$this->assertSame('one point two', Number::spell(1.2));
}

public function testSpelloutWithLocale()
{
$this->needsIntlExtension();

$this->assertSame('trois', Number::spell(3, 'fr'));
}

public function testOrdinal()
{
$this->assertSame('1st', Number::ordinal(1));
$this->assertSame('2nd', Number::ordinal(2));
$this->assertSame('3rd', Number::ordinal(3));
}

public function testToPercent()
{
$this->needsIntlExtension();
Expand Down

0 comments on commit d9c328c

Please sign in to comment.