@@ -92,12 +92,11 @@ public static function create($value, $scale = null)
92
92
93
93
/**
94
94
* @param integer $intValue
95
- * @param integer $scale
96
95
* @return Decimal
97
96
*/
98
- public static function fromInteger ($ intValue, $ scale = null )
97
+ public static function fromInteger ($ intValue )
99
98
{
100
- self ::paramsValidation ($ intValue , $ scale );
99
+ self ::paramsValidation ($ intValue , null );
101
100
102
101
if (!is_int ($ intValue )) {
103
102
throw new InvalidArgumentTypeException (
@@ -107,10 +106,7 @@ public static function fromInteger($intValue, $scale = null)
107
106
);
108
107
}
109
108
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 );
114
110
}
115
111
116
112
/**
@@ -140,14 +136,10 @@ public static function fromFloat($fltValue, $scale = null)
140
136
);
141
137
}
142
138
143
- $ dec_scale = $ scale === null ?
144
- 8 :
145
- $ scale ;
139
+ $ dec_scale = $ scale === null ? 8 : $ scale ;
140
+ $ strValue = self ::floatToString ($ fltValue , $ dec_scale );
146
141
147
- return new Decimal (
148
- number_format ($ fltValue , $ dec_scale , '. ' , '' ),
149
- $ dec_scale
150
- );
142
+ return new Decimal ($ strValue , $ dec_scale );
151
143
}
152
144
153
145
/**
@@ -981,8 +973,24 @@ private static function normalizeSign($sign)
981
973
return $ sign ;
982
974
}
983
975
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
+
984
991
/**
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).
986
994
*
987
995
* @param Decimal $val
988
996
* @param Decimal $abs $val->abs()
0 commit comments