Skip to content

Commit

Permalink
Add negative function.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Mar 21, 2023
1 parent ac0d625 commit 9cf872b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
# Node.js
/node_modules/
/package-lock.json

# PHPUnit
/.phpunit.result.cache
10 changes: 10 additions & 0 deletions src/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ public function is_zero() {
return 0 === $calculator->compare( $this, new self( '0' ) );
}

/**
* Negative.
*
* @link https://github.com/pronamic/wp-number/issues/1
* @return Number
*/
public function negative() {
return Number::from_int( 0 )->subtract( $this );
}

/**
* JSON serialize.
*
Expand Down
15 changes: 14 additions & 1 deletion tests/src/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,20 @@ public function test_to_string() {
$this->assertSame( '123456.78', \strval( $number ) );
}

/**
* Test negative.
*
* @link https://github.com/pronamic/wp-number/issues/1
*/
public function test_negative() {
$number = new Number( 29.95 );

$negative = $number->negative();

$this->assertSame( '29.95', \strval( $number ) );
$this->assertSame( '-29.95', \strval( $negative ) );
}

/**
* Test calculator exception.
*/
Expand Down Expand Up @@ -448,7 +462,6 @@ public function test_calculator() {
$calculators_property->setValue( $calculators );
}


/**
* Test format i18n.
*
Expand Down

0 comments on commit 9cf872b

Please sign in to comment.