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 Add<BigDecimalRef<'_>> for &BigInt
impl Add<BigDecimalRef<'_>> 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<BigDecimalRef<'_>> for BigInt
impl Add<BigDecimalRef<'_>> 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 moreSource§impl ToPrimitive for BigDecimalRef<'_>
impl ToPrimitive for BigDecimalRef<'_>
Source§fn to_i64(&self) -> Option<i64>
fn to_i64(&self) -> Option<i64>
self
to an i64
. If the value cannot be
represented by an i64
, then None
is returned.Source§fn to_i128(&self) -> Option<i128>
fn to_i128(&self) -> Option<i128>
self
to an i128
. If the value cannot be
represented by an i128
(i64
under the default implementation), then
None
is returned. Read moreSource§fn to_u64(&self) -> Option<u64>
fn to_u64(&self) -> Option<u64>
self
to a u64
. If the value cannot be
represented by a u64
, then None
is returned.Source§fn to_u128(&self) -> Option<u128>
fn to_u128(&self) -> Option<u128>
self
to a u128
. If the value cannot be
represented by a u128
(u64
under the default implementation), then
None
is returned. Read moreSource§fn to_f64(&self) -> Option<f64>
fn to_f64(&self) -> Option<f64>
self
to an f64
. Overflows may map to positive
or negative inifinity, otherwise None
is returned if the value cannot
be represented by an f64
. Read moreSource§fn to_isize(&self) -> Option<isize>
fn to_isize(&self) -> Option<isize>
self
to an isize
. If the value cannot be
represented by an isize
, then None
is returned.Source§fn to_i8(&self) -> Option<i8>
fn to_i8(&self) -> Option<i8>
self
to an i8
. If the value cannot be
represented by an i8
, then None
is returned.Source§fn to_i16(&self) -> Option<i16>
fn to_i16(&self) -> Option<i16>
self
to an i16
. If the value cannot be
represented by an i16
, then None
is returned.Source§fn to_i32(&self) -> Option<i32>
fn to_i32(&self) -> Option<i32>
self
to an i32
. If the value cannot be
represented by an i32
, then None
is returned.Source§fn to_usize(&self) -> Option<usize>
fn to_usize(&self) -> Option<usize>
self
to a usize
. If the value cannot be
represented by a usize
, then None
is returned.Source§fn to_u8(&self) -> Option<u8>
fn to_u8(&self) -> Option<u8>
self
to a u8
. If the value cannot be
represented by a u8
, then None
is returned.Source§fn to_u16(&self) -> Option<u16>
fn to_u16(&self) -> Option<u16>
self
to a u16
. If the value cannot be
represented by a u16
, then None
is returned.