From 8e0735fd44721522fde26700e614e95c4bccdb79 Mon Sep 17 00:00:00 2001 From: Andrew Kubera Date: Wed, 19 Jun 2024 02:13:34 -0400 Subject: [PATCH] Add methods BigDecimal::{into,as}_bigint_and_scale --- src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 57495d7..e7cfc87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -88,6 +88,7 @@ use self::stdlib::str::FromStr; use self::stdlib::string::{String, ToString}; use self::stdlib::fmt; use self::stdlib::Vec; +use self::stdlib::borrow::Cow; use num_bigint::{BigInt, BigUint, ParseBigIntError, Sign}; use num_integer::Integer as IntegerTrait; @@ -572,6 +573,24 @@ impl BigDecimal { (self.int_val.clone(), self.scale) } + /// Take BigDecimal and split into `num::BigInt` of digits, and the scale + /// + /// Scale is number of digits after the decimal point, can be negative. + /// + pub fn into_bigint_and_scale(self) -> (BigInt, i64) { + (self.int_val, self.scale) + } + + + /// Return digits as borrowed Cow of integer digits, and its scale + /// + /// Scale is number of digits after the decimal point, can be negative. + /// + pub fn as_bigint_and_scale(&self) -> (Cow<'_, BigInt>, i64) { + let cow_int = Cow::Borrowed(&self.int_val); + (cow_int, self.scale) + } + /// Convert into the internal big integer value and an exponent. Note that a positive /// exponent indicates a negative power of 10. ///