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

Commit 9501c65

Browse files
committed
Added more flexibility to the Decimal::fromString when receives exponential notation
1 parent 1aee926 commit 9501c65

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Decimal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public static function fromString($strValue, $scale = null)
193193
$scale :
194194
(isset($captures[4]) ? max(0, strlen($captures[4])-1) : 0);
195195

196-
} elseif (preg_match('/([+\-]?)([0-9](\.[0-9]+)?)[eE]([+\-]?)([1-9][0-9]*)/', $strValue, $captures) === 1) {
196+
} elseif (preg_match('/([+\-]?)0*([0-9](\.[0-9]+)?)[eE]([+\-]?)([1-9][0-9]*)/', $strValue, $captures) === 1) {
197197

198198
// Now it's time to "unroll" the exponential notation to basic positional notation
199199
$sign = self::normalizeSign($captures[1]);

tests/Decimal/DecimalFromStringTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testNegativeSimpleString()
2323
$this->assertEquals($n2->__toString(), '-1.0');
2424
}
2525

26-
public function testExponentialNotationString_With_PositiveExponent_And_PositiveSign()
26+
public function testExponentialNotationString_With_PositiveExponent_And_Positive()
2727
{
2828
$this->assertTrue(
2929
Decimal::fromString('1e3')->equals(Decimal::fromInteger(1000))
@@ -45,7 +45,7 @@ public function testExponentialNotationString_With_PositiveExponent_And_Negative
4545
);
4646
}
4747

48-
public function testExponentialNotationString_With_NegativeExponent_And_PositiveSign()
48+
public function testExponentialNotationString_With_NegativeExponent_And_Positive()
4949
{
5050
$this->assertTrue(
5151
Decimal::fromString('1e-3')->equals(Decimal::fromString('0.001'))
@@ -67,7 +67,7 @@ public function testExponentialNotationString_With_NegativeExponent_And_Negative
6767
);
6868
}
6969

70-
public function testSimpleNotation_With_Positive_Sign()
70+
public function testSimpleNotation_With_PositiveSign()
7171
{
7272
$this->assertTrue(
7373
Decimal::fromString('+34')->equals(Decimal::fromString('34'))
@@ -78,6 +78,17 @@ public function testSimpleNotation_With_Positive_Sign()
7878
);
7979
}
8080

81+
public function testExponentialNotation_With_PositiveSign()
82+
{
83+
$this->assertTrue(
84+
Decimal::fromString('+1e3')->equals(Decimal::fromString('1e3'))
85+
);
86+
87+
$this->assertTrue(
88+
Decimal::fromString('+0001e3')->equals(Decimal::fromString('1e3'))
89+
);
90+
}
91+
8192
/**
8293
* @expectedException Litipk\Exceptions\InvalidArgumentTypeException
8394
* @expectedExceptionMessage $strVlue must be of type string.

0 commit comments

Comments
 (0)