[−][src]Struct commodity::Commodity
A commodity, which holds a value of a type of Currrency
Fields
value: Decimal
currency_code: CurrencyCode
Methods
impl Commodity
[src]
pub fn new<T: Into<CurrencyCode>>(value: Decimal, currency_code: T) -> Commodity
[src]
Create a new Commodity.
Example
use commodity::CurrencyCode; use std::str::FromStr; use rust_decimal::Decimal; let currency_code = CurrencyCode::from_str("USD").unwrap(); let commodity = Commodity::new(Decimal::new(202, 2), currency_code); assert_eq!(Decimal::from_str("2.02").unwrap(), commodity.value); assert_eq!(currency_code, commodity.currency_code)
Using using the Into
trait to accept Currency
as the currency_code
:
use std::str::FromStr; use commodity::Currency; use rust_decimal::Decimal; let currency = Currency::from_alpha3("USD").unwrap(); let commodity = Commodity::new(Decimal::new(202, 2), ¤cy);
pub fn zero(currency_code: CurrencyCode) -> Commodity
[src]
Create a commodity with a value of zero
pub fn add(&self, other: &Commodity) -> Result<Commodity, CommodityError>
[src]
Add the value of commodity other
to self
such that result = self + other
.
Example
use rust_decimal::Decimal; use std::str::FromStr; let currency_code = CurrencyCode::from_str("USD").unwrap(); let commodity1 = Commodity::new(Decimal::new(400, 2), currency_code); let commodity2 = Commodity::new(Decimal::new(250, 2), currency_code); // perform the add let result = commodity1.add(&commodity2).unwrap(); assert_eq!(Decimal::new(650, 2), result.value); assert_eq!(currency_code, result.currency_code);
pub fn sub(&self, other: &Commodity) -> Result<Commodity, CommodityError>
[src]
Subtract the value of commodity other
from self
such that result = self - other
.
Example
use rust_decimal::Decimal; use std::str::FromStr; let currency_code = CurrencyCode::from_str("USD").unwrap(); let commodity1 = Commodity::new(Decimal::new(400, 2), currency_code); let commodity2 = Commodity::new(Decimal::new(250, 2), currency_code); // perform the subtraction let result = commodity1.sub(&commodity2).unwrap(); assert_eq!(Decimal::new(150, 2), result.value); assert_eq!(currency_code, result.currency_code);
pub fn neg(&self) -> Commodity
[src]
Negate the value of this commodity such that result = -self
Example
use rust_decimal::Decimal; let currency_code = CurrencyCode::from_str("USD").unwrap(); let commodity = Commodity::new(Decimal::new(202, 2), currency_code); // perform the negation let result = commodity.neg(); assert_eq!(Decimal::from_str("-2.02").unwrap(), result.value); assert_eq!(currency_code, result.currency_code)
pub fn div_i64(&self, i: i64) -> Commodity
[src]
Divide this commodity by the specified integer value
Example
use rust_decimal::{Decimal}; use std::str::FromStr; let commodity = Commodity::from_str("4.03 AUD").unwrap(); let result = commodity.div_i64(4); assert_eq!(Decimal::new(10075, 4), result.value);
pub fn divide_share(&self, i: i64, dp: u32) -> Vec<Commodity>
[src]
Divide this commodity by the specified integer value
Example
use rust_decimal::{Decimal}; use std::str::FromStr; let commodity = Commodity::from_str("4.03 AUD").unwrap(); let results = commodity.divide_share(4, 2); assert_eq!(Decimal::new(101, 2), results.get(0).unwrap().value); assert_eq!(Decimal::new(101, 2), results.get(1).unwrap().value); assert_eq!(Decimal::new(101, 2), results.get(2).unwrap().value); assert_eq!(Decimal::new(100, 2), results.get(3).unwrap().value);
pub fn convert(&self, currency_code: CurrencyCode, rate: Decimal) -> Commodity
[src]
Convert this commodity to a different currency using a conversion rate.
Example
use rust_decimal::Decimal; use std::str::FromStr; let aud = Commodity::from_str("100.00 AUD").unwrap(); let usd = aud.convert(CurrencyCode::from_str("USD").unwrap(), Decimal::from_str("0.01").unwrap()); assert_eq!(Decimal::from_str("1.00").unwrap(), usd.value); assert_eq!("USD", usd.currency_code);
pub fn compatible_with(&self, other: &Commodity) -> bool
[src]
Returns true if the currencies of both this commodity, and
the other
commodity are compatible for numeric operations.
Example
use std::str::FromStr; let aud1 = Commodity::from_str("1.0 AUD").unwrap(); let aud2 = Commodity::from_str("2.0 AUD").unwrap(); let nzd = Commodity::from_str("1.0 NZD").unwrap(); assert!(aud1.compatible_with(&aud2)); assert!(!aud1.compatible_with(&nzd));
pub fn lt(&self, other: &Commodity) -> Result<bool, CommodityError>
[src]
Compare whether this commodity has a value less than another commodity.
Will return an error if the commodities have incompatible currencies.
Example
use std::str::FromStr; let aud1 = Commodity::from_str("1.0 AUD").unwrap(); let aud2 = Commodity::from_str("2.0 AUD").unwrap(); assert_eq!(true, aud1.lt(&aud2).unwrap()); assert_eq!(false, aud2.lt(&aud1).unwrap());
pub fn gt(&self, other: &Commodity) -> Result<bool, CommodityError>
[src]
Compare whether this commodity has a value greater than another commodity.
Will return an error if the commodities have incompatible currencies.
Example
use std::str::FromStr; let aud1 = Commodity::from_str("1.0 AUD").unwrap(); let aud2 = Commodity::from_str("2.0 AUD").unwrap(); assert_eq!(false, aud1.gt(&aud2).unwrap()); assert_eq!(true, aud2.gt(&aud1).unwrap());
pub fn abs(&self) -> Commodity
[src]
Return the absolute value of this commodity (if the value is negative, then make it positive).
Example
use std::str::FromStr; let aud1 = Commodity::from_str("-1.0 AUD").unwrap(); assert_eq!(Commodity::from_str("1.0 AUD").unwrap(), aud1.abs()); let aud2 = Commodity::from_str("2.0 AUD").unwrap(); assert_eq!(Commodity::from_str("2.0 AUD").unwrap(), aud2.abs());
pub fn default_epsilon() -> Decimal
[src]
The default epsilon to use for comparisons between different Commoditys.
pub fn eq_approx(&self, other: Commodity, epsilon: Decimal) -> bool
[src]
Trait Implementations
impl Clone for Commodity
[src]
impl Copy for Commodity
[src]
impl Debug for Commodity
[src]
impl Display for Commodity
[src]
impl Eq for Commodity
[src]
impl FromStr for Commodity
[src]
type Err = CommodityError
The associated error which can be returned from parsing.
fn from_str(commodity_string: &str) -> Result<Commodity, CommodityError>
[src]
impl Ord for Commodity
[src]
fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
[src]
impl PartialEq<Commodity> for Commodity
[src]
impl PartialOrd<Commodity> for Commodity
[src]
fn partial_cmp(&self, other: &Commodity) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl StructuralEq for Commodity
[src]
impl StructuralPartialEq for Commodity
[src]
Auto Trait Implementations
impl RefUnwindSafe for Commodity
impl Send for Commodity
impl Sync for Commodity
impl Unpin for Commodity
impl UnwindSafe for Commodity
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,