Skip to content

Commit

Permalink
[10.x] Fixing number helper for floating 0.0 (#49277)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MrPunyapal authored Dec 9, 2023
1 parent 1771cd5 commit c3696ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit c3696ca

Please sign in to comment.