From 7261aafb6c84a9c522a1d0d8afdce0034f456bf1 Mon Sep 17 00:00:00 2001 From: Ashot Gharakeshishyan Date: Fri, 30 Aug 2024 23:35:18 +0400 Subject: [PATCH] add @log directive --- src/Illuminate/Support/Number.php | 36 +++++++++++++++++++++++++++++ tests/Support/SupportNumberTest.php | 18 +++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index 7719055f6224..24825cdd7308 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -232,6 +232,42 @@ public static function clamp(int|float $number, int|float $min, int|float $max) return min(max($number, $min), $max); } + /** + * Split the given number into pairs of min/max values. + * + * @param int|float $to + * @param int|float $by + * @param int|float $offset + * @return array + */ + public static function pairs(int|float $to, int|float $by, int|float $offset = 1) + { + $output = []; + + for ($lower = 0; $lower < $to; $lower += $by) { + $upper = $lower + $by; + + if ($upper > $to) { + $upper = $to; + } + + $output[] = [$lower + $offset, $upper]; + } + + return $output; + } + + /** + * Remove any trailing zero digits after the decimal point of the given number. + * + * @param int|float $number + * @return int|float + */ + public static function trim(int|float $number) + { + return json_decode(json_encode($number)); + } + /** * Execute the given callback using the given locale. * diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index e54c03d77d1c..bc4d21b1239c 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -286,4 +286,22 @@ public function testSummarize() $this->assertSame('-1Q', Number::abbreviate(-1000000000000000)); $this->assertSame('-1KQ', Number::abbreviate(-1000000000000000000)); } + + public function testPairs() + { + $this->assertSame([[1, 10], [11, 20], [21, 25]], Number::pairs(25, 10)); + $this->assertSame([[0, 10], [10, 20], [20, 25]], Number::pairs(25, 10, 0)); + $this->assertSame([[0, 2.5], [2.5, 5.0], [5.0, 7.5], [7.5, 10.0]], Number::pairs(10, 2.5, 0)); + } + + public function testTrim() + { + $this->assertSame(12, Number::trim(12)); + $this->assertSame(120, Number::trim(120)); + $this->assertSame(12, Number::trim(12.0)); + $this->assertSame(12.3, Number::trim(12.3)); + $this->assertSame(12.3, Number::trim(12.30)); + $this->assertSame(12.3456789, Number::trim(12.3456789)); + $this->assertSame(12.3456789, Number::trim(12.34567890000)); + } }