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

Commit 16ba575

Browse files
author
Andrés Correa Casablanca
committed
Fix issue #58
1 parent 9028f98 commit 16ba575

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Decimal.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ public static function fromString(string $strValue, int $scale = null): Decimal
146146
$scale = $scale ?? $min_scale;
147147
if ($scale < $min_scale) {
148148
$value = self::innerRound($value, $scale);
149+
} elseif ($min_scale < $scale) {
150+
$hasPoint = (false !== \strpos($value, '.'));
151+
$value .= ($hasPoint ? '' : '.') . \str_pad('', $scale - $min_scale, '0');
149152
}
150153

151154
return new static($value, $scale);

tests/regression/issue58Test.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Litipk\BigNumbers\Decimal;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class issue58Test extends TestCase
9+
{
10+
public function test_that_fromString_preserves_the_correct_inner_scale_to_avoid_divisions_by_zero()
11+
{
12+
$value = Decimal::create('12.99', 4);
13+
$divisor = Decimal::create(2, 4);
14+
15+
$this->assertEquals(6.495, $value->div($divisor)->asFloat());
16+
}
17+
}

0 commit comments

Comments
 (0)