From 54f171f9ed65ce932c436c7f2bfeba51c29fb6ee Mon Sep 17 00:00:00 2001 From: Daniel Ramirez Date: Thu, 16 May 2024 11:06:41 -0400 Subject: [PATCH] feat: add from_fraction_with_precision --- src/decimal/mod.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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 }