Skip to content

Commit

Permalink
feat: add from_fraction_with_precision
Browse files Browse the repository at this point in the history
  • Loading branch information
javamonn authored and dnsl48 committed May 18, 2024
1 parent 837d084 commit 7f8d3df
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,15 @@ where

GenericDecimal(fraction, P::zero()).calc_precision(Some(max_precision))
}

#[inline]
pub fn from_fraction_with_precision(fraction: GenericFraction<T>, precision: P) -> Self
where
T: GenericInteger + ToPrimitive,
P: Bounded + CheckedAdd,
{
GenericDecimal(fraction, precision)
}
}

impl<T, F, P1, P2> TryToConvertFrom<GenericDecimal<F, P1>> for GenericDecimal<T, P2>
Expand Down Expand Up @@ -1166,5 +1175,15 @@ mod tests {
}
}

#[test]
fn from_fraction_with_precision() {
let one_third: GenericFraction<u64> = GenericFraction::new(1u64, 3u64);

assert_eq!(
GenericDecimal::<u64, u8>::from_fraction_with_precision(one_third, 18).get_precision(),
18
);
}

// TODO: more tests
}

0 comments on commit 7f8d3df

Please sign in to comment.