pub struct BigDecimalRef<'a> { /* private fields */ }
Expand description
Immutable big-decimal, referencing a borrowed buffer of digits
The non-digit information like scale
and sign
may be changed
on these objects, which otherwise would require cloning the full
digit buffer in the BigDecimal.
Built from full BigDecimal
object using the to_ref()
method.
BigDecimal
not implement AsRef
, so we will reserve the method
as_ref()
for a later time.
May be transformed into full BigDecimal object using the to_owned()
method.
This clones the bigdecimal digits.
BigDecimalRef (or Into<BigDecimalRef>
) should be preferred over
using &BigDecimal
for library functions that need an immutable
reference to a bigdecimal, as it may be much more efficient.
NOTE: Using &BigDecimalRef
is redundant, and not recommended.
§Examples
fn add_one<'a, N: Into<BigDecimalRef<'a>>>(n: N) -> BigDecimal {
n.into() + 1
}
let n: BigDecimal = "123.456".parse().unwrap();
// call via "standard" reference (implements Into)
let m = add_one(&n);
assert_eq!(m, "124.456".parse().unwrap());
// call by negating the reference (fast: no-digit cloning involved)
let m = add_one(n.to_ref().neg());
assert_eq!(m, "-122.456".parse().unwrap());
Implementations§
Source§impl BigDecimalRef<'_>
impl BigDecimalRef<'_>
Sourcepub fn to_owned(&self) -> BigDecimal
pub fn to_owned(&self) -> BigDecimal
Clone digits to make this reference a full BigDecimal object
Sourcepub fn to_owned_with_scale(&self, scale: i64) -> BigDecimal
pub fn to_owned_with_scale(&self, scale: i64) -> BigDecimal
Clone digits, returning BigDecimal with given scale
let n: BigDecimal = "123.45678".parse().unwrap();
let r = n.to_ref();
assert_eq!(r.to_owned_with_scale(5), n.clone());
assert_eq!(r.to_owned_with_scale(0), "123".parse().unwrap());
assert_eq!(r.to_owned_with_scale(-1), "12e1".parse().unwrap());
let x = r.to_owned_with_scale(8);
assert_eq!(&x, &n);
assert_eq!(x.fractional_digit_count(), 8);
Sourcepub fn fractional_digit_count(&self) -> i64
pub fn fractional_digit_count(&self) -> i64
Return number of digits ‘right’ of the decimal point (including leading zeros)
Sourcepub fn count_digits(&self) -> u64
pub fn count_digits(&self) -> u64
Count total number of decimal digits
Sourcepub fn round_with_context(&self, ctx: &Context) -> BigDecimal
pub fn round_with_context(&self, ctx: &Context) -> BigDecimal
Create BigDecimal from this reference, rounding to precision and with rounding-mode of the given context
Sourcepub fn sqrt_with_context(&self, ctx: &Context) -> Option<BigDecimal>
pub fn sqrt_with_context(&self, ctx: &Context) -> Option<BigDecimal>
Take square root of this number
Sourcepub fn sqrt_abs_with_context(&self, ctx: &Context) -> BigDecimal
pub fn sqrt_abs_with_context(&self, ctx: &Context) -> BigDecimal
Take square root of absolute-value of the number
Sourcepub fn sqrt_copysign_with_context(&self, ctx: &Context) -> BigDecimal
pub fn sqrt_copysign_with_context(&self, ctx: &Context) -> BigDecimal
Take square root, copying sign of the initial decimal
Sourcepub fn clone_into(&self, dest: &mut BigDecimal)
pub fn clone_into(&self, dest: &mut BigDecimal)
Clone this value into dest
Trait Implementations§
Source§impl Add<&i128> for BigDecimalRef<'_>
impl Add<&i128> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<&i16> for BigDecimalRef<'_>
impl Add<&i16> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<&i32> for BigDecimalRef<'_>
impl Add<&i32> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<&i64> for BigDecimalRef<'_>
impl Add<&i64> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<&i8> for BigDecimalRef<'_>
impl Add<&i8> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<&u128> for BigDecimalRef<'_>
impl Add<&u128> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<&u16> for BigDecimalRef<'_>
impl Add<&u16> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<&u32> for BigDecimalRef<'_>
impl Add<&u32> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<&u64> for BigDecimalRef<'_>
impl Add<&u64> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<&u8> for BigDecimalRef<'_>
impl Add<&u8> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<BigDecimal> for BigDecimalRef<'_>
impl Add<BigDecimal> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§fn add(self, rhs: BigDecimal) -> BigDecimal
fn add(self, rhs: BigDecimal) -> BigDecimal
+
operation. Read moreSource§impl<'a> Add<BigDecimalRef<'a>> for &BigInt
impl<'a> Add<BigDecimalRef<'a>> for &BigInt
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§fn add(self, rhs: BigDecimalRef<'_>) -> BigDecimal
fn add(self, rhs: BigDecimalRef<'_>) -> BigDecimal
+
operation. Read moreSource§impl<'a> Add<BigDecimalRef<'a>> for BigInt
impl<'a> Add<BigDecimalRef<'a>> for BigInt
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§fn add(self, rhs: BigDecimalRef<'_>) -> BigDecimal
fn add(self, rhs: BigDecimalRef<'_>) -> BigDecimal
+
operation. Read moreSource§impl Add<BigInt> for BigDecimalRef<'_>
impl Add<BigInt> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl<'a, T: Into<BigDecimalRef<'a>>> Add<T> for BigDecimalRef<'_>
impl<'a, T: Into<BigDecimalRef<'a>>> Add<T> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§fn add(self, rhs: T) -> BigDecimal
fn add(self, rhs: T) -> BigDecimal
+
operation. Read moreSource§impl Add<i128> for BigDecimalRef<'_>
impl Add<i128> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<i16> for BigDecimalRef<'_>
impl Add<i16> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<i32> for BigDecimalRef<'_>
impl Add<i32> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<i64> for BigDecimalRef<'_>
impl Add<i64> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<i8> for BigDecimalRef<'_>
impl Add<i8> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<u128> for BigDecimalRef<'_>
impl Add<u128> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<u16> for BigDecimalRef<'_>
impl Add<u16> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<u32> for BigDecimalRef<'_>
impl Add<u32> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<u64> for BigDecimalRef<'_>
impl Add<u64> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl Add<u8> for BigDecimalRef<'_>
impl Add<u8> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
+
operator.Source§impl<'a> Clone for BigDecimalRef<'a>
impl<'a> Clone for BigDecimalRef<'a>
Source§fn clone(&self) -> BigDecimalRef<'a>
fn clone(&self) -> BigDecimalRef<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'a> Debug for BigDecimalRef<'a>
impl<'a> Debug for BigDecimalRef<'a>
Source§impl Display for BigDecimalRef<'_>
impl Display for BigDecimalRef<'_>
Source§impl<'a> From<&'a BigDecimal> for BigDecimalRef<'a>
impl<'a> From<&'a BigDecimal> for BigDecimalRef<'a>
Source§fn from(n: &'a BigDecimal) -> Self
fn from(n: &'a BigDecimal) -> Self
Source§impl<'a> From<&'a BigInt> for BigDecimalRef<'a>
impl<'a> From<&'a BigInt> for BigDecimalRef<'a>
Source§impl LowerExp for BigDecimalRef<'_>
impl LowerExp for BigDecimalRef<'_>
Source§impl Neg for BigDecimalRef<'_>
impl Neg for BigDecimalRef<'_>
Source§impl Ord for BigDecimalRef<'_>
impl Ord for BigDecimalRef<'_>
Source§fn cmp(&self, other: &BigDecimalRef<'_>) -> Ordering
fn cmp(&self, other: &BigDecimalRef<'_>) -> Ordering
Complete ordering implementation for BigDecimal
§Example
use std::str::FromStr;
let a = bigdecimal::BigDecimal::from_str("-1").unwrap();
let b = bigdecimal::BigDecimal::from_str("1").unwrap();
assert!(a < b);
assert!(b > a);
let c = bigdecimal::BigDecimal::from_str("1").unwrap();
assert!(b >= c);
assert!(c >= b);
let d = bigdecimal::BigDecimal::from_str("10.0").unwrap();
assert!(d > c);
let e = bigdecimal::BigDecimal::from_str(".5").unwrap();
assert!(e < c);
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<'rhs, T> PartialEq<T> for BigDecimalRef<'_>
impl<'rhs, T> PartialEq<T> for BigDecimalRef<'_>
Source§impl PartialOrd for BigDecimalRef<'_>
impl PartialOrd for BigDecimalRef<'_>
Source§impl Sub<BigDecimal> for BigDecimalRef<'_>
impl Sub<BigDecimal> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
-
operator.Source§fn sub(self, rhs: BigDecimal) -> BigDecimal
fn sub(self, rhs: BigDecimal) -> BigDecimal
-
operation. Read moreSource§impl<'a> Sub<BigDecimalRef<'a>> for &BigInt
impl<'a> Sub<BigDecimalRef<'a>> for &BigInt
Source§type Output = BigDecimal
type Output = BigDecimal
-
operator.Source§fn sub(self, rhs: BigDecimalRef<'a>) -> BigDecimal
fn sub(self, rhs: BigDecimalRef<'a>) -> BigDecimal
-
operation. Read moreSource§impl<'a> Sub<BigDecimalRef<'a>> for BigInt
impl<'a> Sub<BigDecimalRef<'a>> for BigInt
Source§type Output = BigDecimal
type Output = BigDecimal
-
operator.Source§fn sub(self, rhs: BigDecimalRef<'a>) -> BigDecimal
fn sub(self, rhs: BigDecimalRef<'a>) -> BigDecimal
-
operation. Read moreSource§impl Sub<BigInt> for BigDecimalRef<'_>
impl Sub<BigInt> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
-
operator.Source§impl<'a, T: Into<BigDecimalRef<'a>>> Sub<T> for BigDecimalRef<'_>
impl<'a, T: Into<BigDecimalRef<'a>>> Sub<T> for BigDecimalRef<'_>
Source§type Output = BigDecimal
type Output = BigDecimal
-
operator.Source§fn sub(self, rhs: T) -> BigDecimal
fn sub(self, rhs: T) -> BigDecimal
-
operation. Read more