Skip to content

Commit b4f8cb5

Browse files
authored
Release/3.4.0 (#25)
1 parent 5819053 commit b4f8cb5

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/BigNumber.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ interface BigNumber
1414
{
1515
public const AUTOMATIC_SCALE = null;
1616

17+
/**
18+
* Converts the current BigNumber to its absolute value.
19+
*
20+
* @return BigNumber A new BigNumber representing the absolute value.
21+
*/
22+
public function absolute(): BigNumber;
23+
1724
/**
1825
* Adds the current BigNumber (augend) with another BigNumber (addend).
1926
*

src/Internal/BigNumberBehavior.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ abstract public static function fromFloat(float $value, ?int $scale = BigNumber:
2727

2828
abstract public static function fromString(string $value, ?int $scale = BigNumber::AUTOMATIC_SCALE): BigNumber;
2929

30+
public function absolute(): BigNumber
31+
{
32+
return static::fromString(value: (string)abs((float)$this->number->value));
33+
}
34+
3035
public function add(BigNumber $addend): BigNumber
3136
{
3237
$result = $this->mathOperations->add(augend: $this, addend: $addend);

tests/BigNumberTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111

1212
final class BigNumberTest extends TestCase
1313
{
14+
public function testAbsolute(): void
15+
{
16+
/** @Given a BigNumber instance */
17+
$negativeValue = -10.155;
18+
$number = LargeNumber::fromFloat(value: $negativeValue);
19+
20+
/** @When calling the absolute method */
21+
$actual = $number->absolute();
22+
23+
/** @Then the result should be an instance of BigNumber */
24+
self::assertInstanceOf(BigNumber::class, $actual);
25+
26+
/** @And the value should be the absolute value of the negative number */
27+
self::assertSame(abs($negativeValue), $actual->toFloat());
28+
self::assertSame(sprintf('%s', abs($negativeValue)), $actual->toString());
29+
}
30+
1431
#[DataProvider('providerForTestAdd')]
1532
public function testAdd(int $scale, mixed $value, mixed $other, array $expected): void
1633
{

0 commit comments

Comments
 (0)