Skip to content

Commit

Permalink
New method: hasNonZeroFractionalPart()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Aug 20, 2018
1 parent a92494c commit e599ac0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/BigDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,16 @@ public function getFractionalPart() : string
return substr($value, -$this->scale);
}

/**
* Returns whether this decimal number has a non-zero fractional part.
*
* @return bool
*/
public function hasNonZeroFractionalPart() : bool
{
return $this->getFractionalPart() !== str_repeat('0', $this->scale);
}

/**
* {@inheritdoc}
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/BigDecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,32 @@ public function providerGetFractionalPart()
];
}

/**
* @dataProvider providerHasNonZeroFractionalPart
*
* @param string $number The number to test.
* @param bool $hasNonZeroFractionalPart The expected return value.
*/
public function testHasNonZeroFractionalPart($number, $hasNonZeroFractionalPart)
{
$this->assertSame($hasNonZeroFractionalPart, BigDecimal::of($number)->hasNonZeroFractionalPart());
}

/**
* @return array
*/
public function providerHasNonZeroFractionalPart()
{
return [
['1', false],
['1.0', false],
['1.01', true],
['-123456789', false],
['-123456789.0000000000000000000000000000000000000000000000000000000', false],
['-123456789.00000000000000000000000000000000000000000000000000000001', true]
];
}

/**
* @dataProvider providerToBigInteger
*
Expand Down

0 comments on commit e599ac0

Please sign in to comment.