From 7cfa92ade7a6e4dfc1cdfb96c866528f3d33e5dd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 20:12:04 +0100 Subject: [PATCH] Add a `Number::toHuman()` helper This is unfortunately dependent on the `ext-intl`, as it wraps the NumberFormatter class. --- src/Illuminate/Support/Number.php | 15 +++++++++++++ tests/Support/SupportNumberTest.php | 34 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index 670dbc68f141..f8f8d65adf1a 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -2,12 +2,27 @@ namespace Illuminate\Support; +use NumberFormatter; use Illuminate\Support\Traits\Macroable; class Number { use Macroable; + /** + * Format the number to a fluent human-readable string. + * + * @param float|int $number + * @param string $locale + * @return false|string + */ + public static function toHuman($number, $locale = 'en') + { + $formatter = new NumberFormatter($locale, NumberFormatter::SPELLOUT); + + return $formatter->format($number); + } + /** * Format the number of bytes to a human-readable string. * diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index ab413275e38a..39e11e614593 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -7,6 +7,40 @@ class SupportNumberTest extends TestCase { + public function testToHuman() + { + $this->assertSame('zero', Number::toHuman(0)); + $this->assertSame('one', Number::toHuman(1)); + $this->assertSame('ten', Number::toHuman(10)); + $this->assertSame('twenty-five', Number::toHuman(25)); + $this->assertSame('one hundred', Number::toHuman(100)); + $this->assertSame('one hundred thousand', Number::toHuman(100000)); + $this->assertSame('one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine', Number::toHuman(123456789)); + + $this->assertSame('one billion', Number::toHuman(1000000000)); + $this->assertSame('one trillion', Number::toHuman(1000000000000)); + $this->assertSame('one quadrillion', Number::toHuman(1000000000000000)); + $this->assertSame('1,000,000,000,000,000,000', Number::toHuman(1000000000000000000)); + + $this->assertSame('minus one', Number::toHuman(-1)); + $this->assertSame('minus ten', Number::toHuman(-10)); + $this->assertSame('minus twenty-five', Number::toHuman(-25)); + + $this->assertSame('zero point two', Number::toHuman(0.2)); + $this->assertSame('one point two three', Number::toHuman(1.23)); + $this->assertSame('minus one point two three', Number::toHuman(-1.23)); + $this->assertSame('one hundred twenty-three point four five six', Number::toHuman(123.456)); + } + + public function testToHumanWithDifferentLocale() + { + $this->assertSame('cent vingt-trois', Number::toHuman(123, 'fr')); + + $this->assertSame('ein­hundert­drei­und­zwanzig', Number::toHuman(123, 'de')); + + $this->assertSame('ett­hundra­tjugo­tre', Number::toHuman(123, 'sv')); + } + public function testBytesToHuman() { $this->assertSame('0 B', Number::bytesToHuman(0));