@@ -48,17 +48,16 @@ private function __clone()
48
48
*
49
49
* @param mixed $value
50
50
* @param int $scale
51
- * @param bool $removeZeros If true then removes trailing zeros from the number representation
52
51
* @return Decimal
53
52
*/
54
- public static function create ($ value , int $ scale = null , bool $ removeZeros = false ): Decimal
53
+ public static function create ($ value , int $ scale = null ): Decimal
55
54
{
56
55
if (\is_int ($ value )) {
57
56
return self ::fromInteger ($ value );
58
57
} elseif (\is_float ($ value )) {
59
- return self ::fromFloat ($ value , $ scale, $ removeZeros );
58
+ return self ::fromFloat ($ value , $ scale );
60
59
} elseif (\is_string ($ value )) {
61
- return self ::fromString ($ value , $ scale, $ removeZeros );
60
+ return self ::fromString ($ value , $ scale );
62
61
} elseif ($ value instanceof Decimal) {
63
62
return self ::fromDecimal ($ value , $ scale );
64
63
} else {
@@ -79,10 +78,9 @@ public static function fromInteger(int $intValue): Decimal
79
78
/**
80
79
* @param float $fltValue
81
80
* @param int $scale
82
- * @param bool $removeZeros If true then removes trailing zeros from the number representation
83
81
* @return Decimal
84
82
*/
85
- public static function fromFloat (float $ fltValue , int $ scale = null , bool $ removeZeros = false ): Decimal
83
+ public static function fromFloat (float $ fltValue , int $ scale = null ): Decimal
86
84
{
87
85
self ::paramsValidation ($ fltValue , $ scale );
88
86
@@ -92,24 +90,22 @@ public static function fromFloat(float $fltValue, int $scale = null, bool $remov
92
90
throw new NaNInputError ("fltValue can't be NaN " );
93
91
}
94
92
95
- $ defaultScale = self ::DEFAULT_SCALE ;
96
-
97
93
$ strValue = (string ) $ fltValue ;
98
94
if (\preg_match (self ::EXP_NUM_GROUPS_NUMBER_REGEXP , $ strValue , $ capture )) {
99
95
if (null === $ scale ) {
100
96
$ scale = ('- ' === $ capture ['sign ' ])
101
97
? $ capture ['exp ' ] + \strlen ($ capture ['dec ' ])
102
- : $ defaultScale ;
98
+ : self :: DEFAULT_SCALE ;
103
99
}
104
100
$ strValue = \number_format ($ fltValue , $ scale , '. ' , '' );
105
- }
106
-
107
- if (null === $ scale ) {
108
- $ scale = $ defaultScale ;
109
- }
101
+ } else {
102
+ $ naturalScale = \strlen ((string )fmod ($ fltValue , 1.0 )) - 2 - (($ fltValue < 0 ) ? 1 : 0 );
110
103
111
- if ($ removeZeros ) {
112
- $ strValue = self ::removeTrailingZeros ($ strValue , $ scale );
104
+ if (null === $ scale ) {
105
+ $ scale = $ naturalScale ;
106
+ } else {
107
+ $ strValue .= str_pad ('' , $ scale - $ naturalScale , '0 ' );
108
+ }
113
109
}
114
110
115
111
return new static ($ strValue , $ scale );
@@ -118,10 +114,9 @@ public static function fromFloat(float $fltValue, int $scale = null, bool $remov
118
114
/**
119
115
* @param string $strValue
120
116
* @param integer $scale
121
- * @param boolean $removeZeros If true then removes trailing zeros from the number representation
122
117
* @return Decimal
123
118
*/
124
- public static function fromString (string $ strValue , int $ scale = null , bool $ removeZeros = false ): Decimal
119
+ public static function fromString (string $ strValue , int $ scale = null ): Decimal
125
120
{
126
121
self ::paramsValidation ($ strValue , $ scale );
127
122
@@ -144,13 +139,10 @@ public static function fromString(string $strValue, int $scale = null, bool $rem
144
139
throw new NaNInputError ('strValue must be a number ' );
145
140
}
146
141
147
- $ scale = ( null !== $ scale) ? $ scale : $ min_scale ;
142
+ $ scale = $ scale ?? $ min_scale ;
148
143
if ($ scale < $ min_scale ) {
149
144
$ value = self ::innerRound ($ value , $ scale );
150
145
}
151
- if ($ removeZeros ) {
152
- $ value = self ::removeTrailingZeros ($ value , $ scale );
153
- }
154
146
155
147
return new static ($ value , $ scale );
156
148
}
@@ -312,28 +304,25 @@ public function pow(Decimal $b, int $scale = null): Decimal
312
304
if ($ b ->isPositive ()) {
313
305
return Decimal::fromDecimal ($ this , $ scale );
314
306
} else {
315
- throw new \DomainException (
316
- "zero can't be powered to zero or negative numbers. "
317
- );
307
+ throw new \DomainException ("zero can't be powered to zero or negative numbers. " );
318
308
}
319
309
} elseif ($ b ->isZero ()) {
320
310
return DecimalConstants::One ();
321
311
} else if ($ b ->isNegative ()) {
322
312
return DecimalConstants::One ()->div (
323
- $ this ->pow ($ b ->additiveInverse ()), $ scale
313
+ $ this ->pow ($ b ->additiveInverse (), max ($ scale , self ::DEFAULT_SCALE )),
314
+ max ($ scale , self ::DEFAULT_SCALE )
324
315
);
325
- } elseif ($ b ->scale === 0 ) {
326
- $ pow_scale = $ scale === null ?
327
- \max ($ this ->scale , $ b ->scale ) : \max ($ this ->scale , $ b ->scale , $ scale );
316
+ } elseif (0 === $ b ->scale ) {
317
+ $ pow_scale = \max ($ this ->scale , $ b ->scale , $ scale ?? 0 );
328
318
329
319
return self ::fromString (
330
320
\bcpow ($ this ->value , $ b ->value , $ pow_scale +1 ),
331
321
$ pow_scale
332
322
);
333
323
} else {
334
324
if ($ this ->isPositive ()) {
335
- $ pow_scale = $ scale === null ?
336
- \max ($ this ->scale , $ b ->scale ) : \max ($ this ->scale , $ b ->scale , $ scale );
325
+ $ pow_scale = \max ($ this ->scale , $ b ->scale , $ scale ?? 0 );
337
326
338
327
$ truncated_b = \bcadd ($ b ->value , '0 ' , 0 );
339
328
$ remaining_b = \bcsub ($ b ->value , $ truncated_b , $ b ->scale );
@@ -1299,25 +1288,8 @@ private static function normalizeSign(string $sign): string
1299
1288
return $ sign ;
1300
1289
}
1301
1290
1302
- private static function removeTrailingZeros (string $ strValue , int &$ scale ): string
1303
- {
1304
- \preg_match ('/^[+\-]?[0-9]+(\.([0-9]*[1-9])?(0+)?)?$/ ' , $ strValue , $ captures );
1305
-
1306
- if (\count ($ captures ) === 4 ) {
1307
- $ toRemove = \strlen ($ captures [3 ]);
1308
- $ scale = \strlen ($ captures [2 ]);
1309
- $ strValue = \substr (
1310
- $ strValue ,
1311
- 0 ,
1312
- \strlen ($ strValue ) - $ toRemove - (0 === $ scale ? 1 : 0 )
1313
- );
1314
- }
1315
-
1316
- return $ strValue ;
1317
- }
1318
-
1319
1291
/**
1320
- * Counts the number of significative digits of $val.
1292
+ * Counts the number of significant digits of $val.
1321
1293
* Assumes a consistent internal state (without zeros at the end or the start).
1322
1294
*
1323
1295
* @param Decimal $val
0 commit comments