Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit edea342

Browse files
author
Andrés Correa Casablanca
authored
Merge pull request #59 from petrkotek/comparison-methods
Add convenient comparison methods isGreaterThan() etc.
2 parents 16ba575 + 68b2705 commit edea342

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

src/Decimal.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,59 @@ public function comp(Decimal $b, int $scale = null): int
455455
);
456456
}
457457

458+
459+
/**
460+
* Returns true if $this > $b, otherwise false
461+
*
462+
* @param Decimal $b
463+
* @param integer $scale
464+
* @return bool
465+
*/
466+
public function isGreaterThan(Decimal $b, int $scale = null): bool
467+
{
468+
return $this->comp($b, $scale) === 1;
469+
}
470+
471+
/**
472+
* Returns true if $this >= $b
473+
*
474+
* @param Decimal $b
475+
* @param integer $scale
476+
* @return bool
477+
*/
478+
public function isGreaterOrEqualTo(Decimal $b, int $scale = null): bool
479+
{
480+
$comparisonResult = $this->comp($b, $scale);
481+
482+
return $comparisonResult === 1 || $comparisonResult === 0;
483+
}
484+
485+
/**
486+
* Returns true if $this < $b, otherwise false
487+
*
488+
* @param Decimal $b
489+
* @param integer $scale
490+
* @return bool
491+
*/
492+
public function isLessThan(Decimal $b, int $scale = null): bool
493+
{
494+
return $this->comp($b, $scale) === -1;
495+
}
496+
497+
/**
498+
* Returns true if $this <= $b, otherwise false
499+
*
500+
* @param Decimal $b
501+
* @param integer $scale
502+
* @return bool
503+
*/
504+
public function isLessOrEqualTo(Decimal $b, int $scale = null): bool
505+
{
506+
$comparisonResult = $this->comp($b, $scale);
507+
508+
return $comparisonResult === -1 || $comparisonResult === 0;
509+
}
510+
458511
/**
459512
* Returns the element's additive inverse.
460513
* @return Decimal
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
use Litipk\BigNumbers\Decimal;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class DecimalIsGreaterOrEqualToTest extends TestCase
8+
{
9+
public function testGreater()
10+
{
11+
$this->assertTrue(Decimal::fromFloat(1.01)->isGreaterOrEqualTo(Decimal::fromFloat(1.001)));
12+
}
13+
14+
public function testEqual()
15+
{
16+
$this->assertTrue(Decimal::fromFloat(1.001)->isGreaterOrEqualTo(Decimal::fromFloat(1.001)));
17+
}
18+
19+
public function testLess()
20+
{
21+
$this->assertFalse(Decimal::fromFloat(1.001)->isGreaterOrEqualTo(Decimal::fromFloat(1.01)));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
use Litipk\BigNumbers\Decimal;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class DecimalIsGreaterThanTest extends TestCase
8+
{
9+
public function testGreater()
10+
{
11+
$this->assertTrue(Decimal::fromFloat(1.01)->isGreaterThan(Decimal::fromFloat(1.001)));
12+
}
13+
14+
public function testEqual()
15+
{
16+
$this->assertFalse(Decimal::fromFloat(1.001)->isGreaterThan(Decimal::fromFloat(1.001)));
17+
}
18+
19+
public function testLess()
20+
{
21+
$this->assertFalse(Decimal::fromFloat(1.001)->isGreaterThan(Decimal::fromFloat(1.01)));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
use Litipk\BigNumbers\Decimal;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class DecimalIsLessOrEqualToTest extends TestCase
8+
{
9+
public function testGreater()
10+
{
11+
$this->assertFalse(Decimal::fromFloat(1.01)->isLessOrEqualTo(Decimal::fromFloat(1.001)));
12+
}
13+
14+
public function testEqual()
15+
{
16+
$this->assertTrue(Decimal::fromFloat(1.001)->isLessOrEqualTo(Decimal::fromFloat(1.001)));
17+
}
18+
19+
public function testLess()
20+
{
21+
$this->assertTrue(Decimal::fromFloat(1.001)->isLessOrEqualTo(Decimal::fromFloat(1.01)));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
use Litipk\BigNumbers\Decimal;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class DecimalIsLessThanTest extends TestCase
8+
{
9+
public function testGreater()
10+
{
11+
$this->assertFalse(Decimal::fromFloat(1.01)->isLessThan(Decimal::fromFloat(1.001)));
12+
}
13+
14+
public function testEqual()
15+
{
16+
$this->assertFalse(Decimal::fromFloat(1.001)->isLessThan(Decimal::fromFloat(1.001)));
17+
}
18+
19+
public function testLess()
20+
{
21+
$this->assertTrue(Decimal::fromFloat(1.001)->isLessThan(Decimal::fromFloat(1.01)));
22+
}
23+
}

0 commit comments

Comments
 (0)