@@ -53,7 +53,6 @@ impl Decimal {
53
53
Some ( Decimal :: from ( n) )
54
54
}
55
55
56
-
57
56
///Return a new Decimal object equivalent to self,
58
57
/// with internal scaling set to the number specified.
59
58
/// If the new_scale is lower than the current value (indicating a larger power of 10),
@@ -144,7 +143,6 @@ impl From<i128> for Decimal {
144
143
}
145
144
}
146
145
147
-
148
146
impl From < u128 > for Decimal {
149
147
fn from ( arg : u128 ) -> Self {
150
148
Self :: from ( BigDecimal :: from ( arg) )
@@ -181,7 +179,7 @@ impl FromStr for Decimal {
181
179
}
182
180
}
183
181
184
- impl ToPrimitive for Decimal {
182
+ impl ToPrimitive for Decimal {
185
183
fn to_i64 ( & self ) -> Option < i64 > {
186
184
self . 0 . to_i64 ( )
187
185
}
@@ -200,6 +198,38 @@ impl ToPrimitive for Decimal{
200
198
}
201
199
}
202
200
201
+ impl FromPrimitive for Decimal {
202
+ #[ inline]
203
+ fn from_i64 ( n : i64 ) -> Option < Self > {
204
+ Some ( Self :: from ( BigDecimal :: from_i64 ( n) ?) )
205
+ }
206
+
207
+ #[ inline]
208
+ fn from_u64 ( n : u64 ) -> Option < Self > {
209
+ Some ( Self :: from ( BigDecimal :: from_u64 ( n) ?) )
210
+ }
211
+
212
+ #[ inline]
213
+ fn from_i128 ( n : i128 ) -> Option < Self > {
214
+ Some ( Self :: from ( BigDecimal :: from_i128 ( n) ?) )
215
+ }
216
+
217
+ #[ inline]
218
+ fn from_u128 ( n : u128 ) -> Option < Self > {
219
+ Some ( Self :: from ( BigDecimal :: from_u128 ( n) ?) )
220
+ }
221
+
222
+ #[ inline]
223
+ fn from_f32 ( n : f32 ) -> Option < Self > {
224
+ Some ( Self :: from ( BigDecimal :: from_f32 ( n) ?) )
225
+ }
226
+
227
+ #[ inline]
228
+ fn from_f64 ( n : f64 ) -> Option < Self > {
229
+ Some ( Self :: from ( BigDecimal :: from_f64 ( n) ?) )
230
+ }
231
+ }
232
+
203
233
impl Default for Decimal {
204
234
fn default ( ) -> Self {
205
235
Decimal ( BigDecimal :: from ( 0 ) )
@@ -373,11 +403,11 @@ mod test {
373
403
374
404
#[ test]
375
405
fn test_try_from_f64 ( ) {
376
- let f= 1.1 ;
406
+ let f = 1.1 ;
377
407
let v = Decimal :: from_f64 ( f) ;
378
- println ! ( "{:?}" , v) ;
379
- if let Some ( v) =v {
380
- println ! ( "{}" , v. to_string( ) ) ;
408
+ println ! ( "{:?}" , v) ;
409
+ if let Some ( v) = v {
410
+ println ! ( "{}" , v. to_string( ) ) ;
381
411
}
382
412
}
383
413
}
0 commit comments