diff --git a/src/decimal/mod.rs b/src/decimal/mod.rs index 42f6e22f..61189af0 100644 --- a/src/decimal/mod.rs +++ b/src/decimal/mod.rs @@ -999,6 +999,15 @@ where GenericDecimal(fraction, P::zero()).calc_precision(Some(max_precision)) } + + #[inline] + pub fn from_fraction_with_precision(fraction: GenericFraction, precision: P) -> Self + where + T: GenericInteger + ToPrimitive, + P: Bounded + CheckedAdd, + { + GenericDecimal(fraction, precision) + } } impl TryToConvertFrom> for GenericDecimal @@ -1166,5 +1175,15 @@ mod tests { } } + #[test] + fn from_fraction_with_precision() { + let one_third: GenericFraction = GenericFraction::new(1u64, 3u64); + + assert_eq!( + GenericDecimal::::from_fraction_with_precision(one_third, 18).get_precision(), + 18 + ); + } + // TODO: more tests }