pub struct Context { /* private fields */ }Expand description
Mathematical Context
Stores rules for numerical operations, such as how to round and number of digits to keep.
Defaults are defined at compile time, determined by environment variables:
| Variable | Description | default |
|---|---|---|
RUST_BIGDECIMAL_DEFAULT_PRECISION | digit precision | 100 |
RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE | rounding-mode | HalfEven |
It is recommended that the user set explicit values of a Context and not rely on compile time constants, but the option is there if necessary.
Implementations§
Source§impl Context
impl Context
Sourcepub fn new(precision: NonZeroU64, rounding: RoundingMode) -> Self
pub fn new(precision: NonZeroU64, rounding: RoundingMode) -> Self
Create context with precision and rounding mode
Sourcepub fn with_precision(&self, precision: NonZeroU64) -> Self
pub fn with_precision(&self, precision: NonZeroU64) -> Self
Copy context with new precision value
Sourcepub fn with_prec<T: ToPrimitive>(&self, precision: T) -> Option<Self>
pub fn with_prec<T: ToPrimitive>(&self, precision: T) -> Option<Self>
Copy context with new precision value
Sourcepub fn with_rounding_mode(&self, mode: RoundingMode) -> Self
pub fn with_rounding_mode(&self, mode: RoundingMode) -> Self
Copy context with new rounding mode
Sourcepub fn precision(&self) -> NonZeroU64
pub fn precision(&self) -> NonZeroU64
Return maximum precision
Sourcepub fn rounding_mode(&self) -> RoundingMode
pub fn rounding_mode(&self) -> RoundingMode
Return rounding mode
Sourcepub fn round_decimal(&self, n: BigDecimal) -> BigDecimal
pub fn round_decimal(&self, n: BigDecimal) -> BigDecimal
Round decimal to precision in this context, using rounding-mode
Sourcepub fn round_decimal_ref<'a, D: Into<BigDecimalRef<'a>>>(
&self,
n: D,
) -> BigDecimal
pub fn round_decimal_ref<'a, D: Into<BigDecimalRef<'a>>>( &self, n: D, ) -> BigDecimal
Round decimal to precision in this context, using rounding-mode
Sourcepub fn multiply<'a, L, R>(&self, lhs: L, rhs: R) -> BigDecimal
pub fn multiply<'a, L, R>(&self, lhs: L, rhs: R) -> BigDecimal
Multiply two decimals, returning product rounded to this Context’s precision
let x: BigDecimal = "1.5".parse().unwrap();
let y: BigDecimal = "3.1415926".parse().unwrap();
let ctx = Context::default().with_prec(5).unwrap();
let product = ctx.multiply(&x, &y);
// rounds to 5 digits of precision
assert_eq!(product, "4.7124".parse().unwrap());
// does not equal the 'full' precision
assert_ne!(product, "4.71238890".parse().unwrap());Sourcepub fn invert<'a, T: Into<BigDecimalRef<'a>>>(&self, n: T) -> BigDecimal
pub fn invert<'a, T: Into<BigDecimalRef<'a>>>(&self, n: T) -> BigDecimal
Calculate 1/n, rounding at this Context’s precision
If n is zero, return zero.
let x: BigDecimal = "3".parse().unwrap();
let ctx = Context::default().with_prec(5).unwrap();
let one_over_three = ctx.invert(&x);
// rounds to 5 digits of precision
assert_eq!(one_over_three, "0.33333".parse().unwrap());Source§impl Context
impl Context
Sourcepub fn add_refs<'a, 'b, A, B>(&self, a: A, b: B) -> BigDecimal
pub fn add_refs<'a, 'b, A, B>(&self, a: A, b: B) -> BigDecimal
Add two big digit references
Sourcepub fn add_refs_into<'a, 'b, A, B>(&self, a: A, b: B, dest: &mut BigDecimal)
pub fn add_refs_into<'a, 'b, A, B>(&self, a: A, b: B, dest: &mut BigDecimal)
Add two decimal refs, storing value in dest