File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -426,6 +426,21 @@ impl BigDecimal {
426
426
}
427
427
}
428
428
429
+ /// Return this BigDecimal with the given precision, rounding if needed
430
+ pub fn with_precision_round ( & self , prec : stdlib:: num:: NonZeroU64 , round : RoundingMode ) -> BigDecimal {
431
+ let digit_count = self . digits ( ) ;
432
+ let new_prec = prec. get ( ) . to_i64 ( ) ;
433
+ let new_scale = new_prec
434
+ . zip ( digit_count. to_i64 ( ) )
435
+ . map ( |( new_prec, old_prec) | new_prec. checked_sub ( old_prec) )
436
+ . flatten ( )
437
+ . map ( |prec_diff| self . scale . checked_add ( prec_diff) )
438
+ . flatten ( )
439
+ . expect ( "precision overflow" ) ;
440
+
441
+ self . with_scale_round ( new_scale, round)
442
+ }
443
+
429
444
/// Return the sign of the `BigDecimal` as `num::bigint::Sign`.
430
445
///
431
446
/// ```
You can’t perform that action at this time.
0 commit comments