Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Added Number::isBetween() method #52587

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,23 @@ public static function trim(int|float $number)
return json_decode(json_encode($number));
}

/**
* Check if the given number is between given min and max number.
*
* @param int|float $value
* @param int|float $min
* @param int|float $max
* @param bool $inclusive
* @return bool
*/
public static function isBetween(int|float $value, int|float $min, int|float $max, bool $inclusive = true)
{
return match ($inclusive) {
true => $value >= $min && $value <= $max,
false => $value > $min && $value < $max,
};
}

/**
* Execute the given callback using the given locale.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,23 @@ public function testTrim()
$this->assertSame(12.3456789, Number::trim(12.3456789));
$this->assertSame(12.3456789, Number::trim(12.34567890000));
}

public function testBetween()
{
$this->assertTrue(Number::isBetween(5, 1, 10));
$this->assertTrue(Number::isBetween(1, 1, 10));
$this->assertTrue(Number::isBetween(10, 1, 10));
$this->assertFalse(Number::isBetween(0, 1, 10));
$this->assertFalse(Number::isBetween(11, 1, 10));
$this->assertTrue(Number::isBetween(7.5, 1.2, 7.5));
$this->assertTrue(Number::isBetween(7.5, 7.5, 10.5));
$this->assertTrue(Number::isBetween(5, 1, 10, false));
$this->assertFalse(Number::isBetween(1, 1, 10, false));
$this->assertFalse(Number::isBetween(10, 1, 10, false));
$this->assertFalse(Number::isBetween(0, 1, 10, false));
$this->assertFalse(Number::isBetween(11, 1, 10, false));
$this->assertFalse(Number::isBetween(7.5, 1.2, 7.5, false));
$this->assertFalse(Number::isBetween(7.5, 7.5, 10.5, false));
$this->assertTrue(Number::isBetween(7.4, 1.2, 7.5, false));
}
}