From c3696ca14a32592396215c4d276c6bbdf8ef2d0c Mon Sep 17 00:00:00 2001 From: Punyapal Shah <53343069+mr-punyapal@users.noreply.github.com> Date: Sat, 9 Dec 2023 20:48:21 +0530 Subject: [PATCH] [10.x] Fixing number helper for floating 0.0 (#49277) * added test case for 0.0 and 0.00 in Number helper * added int type casting while checking for 0 I have added tests which are failing if we pass 0.0 or 0.00 floating value for 0 giving Division by zero exception... * Fix comparison of float values in summarize method * Refactor number formatting and summarization * Fix formatting in SupportNumberTest.php * Refactor SupportNumberTest abbreviate method --- src/Illuminate/Support/Number.php | 4 ++-- tests/Support/SupportNumberTest.php | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index 954c05875a50..a101e903beb3 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -193,8 +193,8 @@ protected static function summarize(int|float $number, int $precision = 0, ?int } switch (true) { - case $number === 0: - return '0'; + case floatval($number) === 0.0: + return $precision > 0 ? static::format(0, $precision, $maxPrecision) : '0'; case $number < 0: return sprintf('-%s', static::summarize(abs($number), $precision, $maxPrecision, $units)); case $number >= 1e15: diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index ce8463bce324..67644fdcb209 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -12,6 +12,8 @@ public function testFormat() $this->needsIntlExtension(); $this->assertSame('0', Number::format(0)); + $this->assertSame('0', Number::format(0.0)); + $this->assertSame('0', Number::format(0.00)); $this->assertSame('1', Number::format(1)); $this->assertSame('10', Number::format(10)); $this->assertSame('25', Number::format(25)); @@ -193,6 +195,9 @@ public function testToHuman() $this->assertSame('1 thousand quadrillion quadrillion', Number::forHumans(1000000000000000000000000000000000)); $this->assertSame('0', Number::forHumans(0)); + $this->assertSame('0', Number::forHumans(0.0)); + $this->assertSame('0.00', Number::forHumans(0, 2)); + $this->assertSame('0.00', Number::forHumans(0.0, 2)); $this->assertSame('-1', Number::forHumans(-1)); $this->assertSame('-1.00', Number::forHumans(-1, precision: 2)); $this->assertSame('-10', Number::forHumans(-10)); @@ -246,6 +251,9 @@ public function testSummarize() $this->assertSame('1KQQ', Number::abbreviate(1000000000000000000000000000000000)); $this->assertSame('0', Number::abbreviate(0)); + $this->assertSame('0', Number::abbreviate(0.0)); + $this->assertSame('0.00', Number::abbreviate(0, 2)); + $this->assertSame('0.00', Number::abbreviate(0.0, 2)); $this->assertSame('-1', Number::abbreviate(-1)); $this->assertSame('-1.00', Number::abbreviate(-1, precision: 2)); $this->assertSame('-10', Number::abbreviate(-10));