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

Commit

Permalink
Fix issue #58
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrés Correa Casablanca committed Jul 5, 2017
1 parent 9028f98 commit 16ba575
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public static function fromString(string $strValue, int $scale = null): Decimal
$scale = $scale ?? $min_scale;
if ($scale < $min_scale) {
$value = self::innerRound($value, $scale);
} elseif ($min_scale < $scale) {
$hasPoint = (false !== \strpos($value, '.'));
$value .= ($hasPoint ? '' : '.') . \str_pad('', $scale - $min_scale, '0');
}

return new static($value, $scale);
Expand Down
17 changes: 17 additions & 0 deletions tests/regression/issue58Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Litipk\BigNumbers\Decimal;
use PHPUnit\Framework\TestCase;

class issue58Test extends TestCase
{
public function test_that_fromString_preserves_the_correct_inner_scale_to_avoid_divisions_by_zero()
{
$value = Decimal::create('12.99', 4);
$divisor = Decimal::create(2, 4);

$this->assertEquals(6.495, $value->div($divisor)->asFloat());
}
}

0 comments on commit 16ba575

Please sign in to comment.