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

Commit 2e74981

Browse files
committed
Simplified Decimal::fromString
1 parent ab7d0a7 commit 2e74981

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/Decimal.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public static function fromString($strValue, $scale = null)
186186
if (preg_match('/^([+\-]?)0*(([1-9][0-9]*|[0-9])(\.[0-9]+)?)$/', $strValue, $captures) === 1) {
187187

188188
// Now it's time to strip leading zeros in order to normalize inner values
189-
$sign = ($captures[1]==='') ? '+' : $captures[1];
190-
$value = $captures[2];
189+
$sign = $captures[1];
190+
$value = $sign . $captures[2];
191191

192192
$dec_scale = $scale !== null ?
193193
$scale :
@@ -196,7 +196,7 @@ public static function fromString($strValue, $scale = null)
196196
} elseif (preg_match('/([+\-]?)([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
199-
$sign = ($captures[1]==='') ? '+' : $captures[1];
199+
$sign = $captures[1];
200200
$mantissa = $captures[2];
201201

202202
$mantissa_scale = strlen($captures[3]) > 0 ? strlen($captures[3])-1 : 0;
@@ -212,7 +212,7 @@ public static function fromString($strValue, $scale = null)
212212
$tmp_multiplier = bcpow(10, -$exp_val, $exp_val);
213213
}
214214

215-
$value = bcmul($mantissa, $tmp_multiplier, max($min_scale, $scale !== null ? $scale : 0));
215+
$value = $sign . bcmul($mantissa, $tmp_multiplier, max($min_scale, $scale !== null ? $scale : 0));
216216
$dec_scale = $scale !== null ? $scale : $min_scale;
217217

218218
} else {
@@ -221,10 +221,6 @@ public static function fromString($strValue, $scale = null)
221221
);
222222
}
223223

224-
if ($sign === '-') {
225-
$value = '-'.$value;
226-
}
227-
228224
if ($scale !== null) {
229225
$value = self::innerRound($value, $scale);
230226
}

0 commit comments

Comments
 (0)