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

Commit 91e4364

Browse files
committed
Removing trailing zeros in the internal representation when it's possible
1 parent 16d7ec5 commit 91e4364

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/Decimal.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ public static function create($value, $scale = null)
9292

9393
/**
9494
* @param integer $intValue
95-
* @param integer $scale
9695
* @return Decimal
9796
*/
98-
public static function fromInteger($intValue, $scale = null)
97+
public static function fromInteger($intValue)
9998
{
100-
self::paramsValidation($intValue, $scale);
99+
self::paramsValidation($intValue, null);
101100

102101
if (!is_int($intValue)) {
103102
throw new InvalidArgumentTypeException(
@@ -107,10 +106,7 @@ public static function fromInteger($intValue, $scale = null)
107106
);
108107
}
109108

110-
return new Decimal(
111-
$scale === null ? (string)$intValue : bcadd((string)$intValue, '0', $scale),
112-
$scale === null ? 0 : $scale
113-
);
109+
return new Decimal((string)$intValue, 0);
114110
}
115111

116112
/**
@@ -140,14 +136,10 @@ public static function fromFloat($fltValue, $scale = null)
140136
);
141137
}
142138

143-
$dec_scale = $scale === null ?
144-
8 :
145-
$scale;
139+
$dec_scale = $scale === null ? 8 : $scale;
140+
$strValue = self::floatToString($fltValue, $dec_scale);
146141

147-
return new Decimal(
148-
number_format($fltValue, $dec_scale, '.', ''),
149-
$dec_scale
150-
);
142+
return new Decimal($strValue, $dec_scale);
151143
}
152144

153145
/**
@@ -981,8 +973,24 @@ private static function normalizeSign($sign)
981973
return $sign;
982974
}
983975

976+
private static function floatToString($fltValue, &$scale)
977+
{
978+
$strValue = number_format($fltValue, $scale, '.', '');
979+
980+
preg_match('/^[+\-]?[0-9]+(\.([0-9]*[1-9])?(0+)?)?$/', $strValue, $captures);
981+
982+
if (count($captures) === 4) {
983+
$toRemove = strlen($captures[3]);
984+
$scale -= $toRemove;
985+
$strValue = substr($strValue, 0, strlen($strValue)-$toRemove-($scale===0 ? 1 : 0));
986+
}
987+
988+
return $strValue;
989+
}
990+
984991
/**
985-
* Counts the number of significative digits of $val
992+
* Counts the number of significative digits of $val.
993+
* Assumes a consistent internal state (without zeros at the end or the start).
986994
*
987995
* @param Decimal $val
988996
* @param Decimal $abs $val->abs()

tests/Decimal/DecimalInternalValidationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testConstructorNullValueValidation()
2323
*/
2424
public function testConstructorNegativeScaleValidation()
2525
{
26-
Decimal::fromInteger(25, -15);
26+
Decimal::fromString("25", -15);
2727
}
2828

2929
/**
@@ -32,7 +32,7 @@ public function testConstructorNegativeScaleValidation()
3232
*/
3333
public function testConstructorNotIntegerScaleValidation()
3434
{
35-
Decimal::fromInteger(25, "hola mundo");
35+
Decimal::fromString("25", "hola mundo");
3636
}
3737

3838
/**

0 commit comments

Comments
 (0)