diff --git a/src/Decimal.php b/src/Decimal.php index 1490b35..1e8dac7 100644 --- a/src/Decimal.php +++ b/src/Decimal.php @@ -858,9 +858,12 @@ private static function innerRound($value, $scale = 0) $diffDigit = bcsub($value, $rounded, $scale+1); $diffDigit = (int)$diffDigit[strlen($diffDigit)-1]; - if ($diffDigit >= 5) { + if ($diffDigit >= 5 && $value[0] !== '-') { $rounded = bcadd($rounded, bcpow('10', -$scale, $scale), $scale); } + if ($diffDigit >= 5 && $value[0] === '-') { + $rounded = bcsub($rounded, bcpow('10', -$scale, $scale), $scale); + } return $rounded; } diff --git a/tests/Decimal/DecimalCosTest.php b/tests/Decimal/DecimalCosTest.php index 260fe20..5e5706e 100644 --- a/tests/Decimal/DecimalCosTest.php +++ b/tests/Decimal/DecimalCosTest.php @@ -12,7 +12,7 @@ public function cosProvider() { return array( array('1', '0.54030230586814', 14), array('123.123', '-0.82483472946164834', 17), - array('15000000000', '-0.72218064388924347681', 20) + array('15000000000', '-0.72218064388924347683', 20) ); } diff --git a/tests/Decimal/DecimalRoundTest.php b/tests/Decimal/DecimalRoundTest.php index 55c320b..c0a8880 100644 --- a/tests/Decimal/DecimalRoundTest.php +++ b/tests/Decimal/DecimalRoundTest.php @@ -23,6 +23,14 @@ public function testRoundWithDecimals() $this->assertTrue(Decimal::fromString('3.44')->round(1)->equals(Decimal::fromString('3.4'))); } + public function testNegativeRoundWithDecimals() + { + $this->assertTrue(Decimal::fromString('-5.59059956723478932512')->round(3)->equals(Decimal::fromString('-5.591'))); + $this->assertTrue(Decimal::fromString('-5.59059956723478932512')->round(4)->equals(Decimal::fromString('-5.5906'))); + $this->assertTrue(Decimal::fromString('-5.59059956723478932512')->round(5)->equals(Decimal::fromString('-5.59060'))); + $this->assertTrue(Decimal::fromString('-5.59059956723478932512')->round(6)->equals(Decimal::fromString('-5.590600'))); + } + public function testNoUsefulRound() { $this->assertTrue(Decimal::fromString('3.45')->round(2)->equals(Decimal::fromString('3.45')));