pub struct Money {
pub amount: Decimal,
pub currency: Currency,
}
Expand description
Fields§
§amount: Decimal
§currency: Currency
Implementations§
source§impl Money
impl Money
sourcepub fn checked_add(self, rhs: Self) -> Option<Self>
pub fn checked_add(self, rhs: Self) -> Option<Self>
Returns Some
if rhs
is the same Currency
and doesn’t over/underflow.
Examples
use money2::{Currency, Money};
let a = Money::new(20_00, 2, Currency::Usd);
assert_eq!(a.checked_add(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
a.checked_add(Money::new(5, 0, Currency::Usd)),
Some(Money::new(25, 0, Currency::Usd))
);
sourcepub fn checked_div(self, rhs: Self) -> Option<Self>
pub fn checked_div(self, rhs: Self) -> Option<Self>
Returns Some
if rhs
is the same Currency
and doesn’t over/underflow.
Examples
use money2::{Currency, Money};
let a = Money::new(20_00, 2, Currency::Usd);
assert_eq!(a.checked_div(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
a.checked_div(Money::new(2, 0, Currency::Usd)),
Some(Money::new(10, 0, Currency::Usd))
);
sourcepub fn checked_mul(self, rhs: Self) -> Option<Self>
pub fn checked_mul(self, rhs: Self) -> Option<Self>
Returns Some
if rhs
is the same Currency
and doesn’t over/underflow.
Examples
use money2::{Currency, Money};
let a = Money::new(20_00, 2, Currency::Usd);
assert_eq!(a.checked_mul(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
a.checked_mul(Money::new(2, 0, Currency::Usd)),
Some(Money::new(40, 0, Currency::Usd))
);
sourcepub fn checked_rem(self, rhs: Self) -> Option<Self>
pub fn checked_rem(self, rhs: Self) -> Option<Self>
Returns Some
if rhs
is the same Currency
and doesn’t over/underflow.
Examples
use money2::{Currency, Money};
let a = Money::new(20_00, 2, Currency::Usd);
assert_eq!(a.checked_rem(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
a.checked_rem(Money::new(3, 0, Currency::Usd)),
Some(Money::new(2, 0, Currency::Usd))
);
sourcepub fn checked_sub(self, rhs: Self) -> Option<Self>
pub fn checked_sub(self, rhs: Self) -> Option<Self>
Returns Some
if rhs
is the same Currency
and doesn’t over/underflow.
Examples
use money2::{Currency, Money};
let a = Money::new(20_00, 2, Currency::Usd);
assert_eq!(a.checked_sub(Money::new(1, 0, Currency::Eur)), None);
assert_eq!(
a.checked_sub(Money::new(5, 0, Currency::Usd)),
Some(Money::new(15, 0, Currency::Usd))
);
Trait Implementations§
source§impl Add for Money
impl Add for Money
source§fn add(self, rhs: Self) -> Self::Output
fn add(self, rhs: Self) -> Self::Output
Panics
- If this currency and the
rhs
’s currency are not the same. - When [
Decimal::add
] does.
See also
Examples
use money2::{Currency, Money};
assert_eq!(
Money::new(10, 0, Currency::Eur) + Money::new(0_50, 2, Currency::Eur),
Money::new(10_50, 2, Currency::Eur)
);
ⓘ
let _ = Money::new(10, 0, Currency::Eur) + Money::new(0_50, 2, Currency::Usd);
source§impl AddAssign for Money
impl AddAssign for Money
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Panics
- When
Money::add
does.
See also
Examples
use money2::{Currency, Money};
let mut foo = Money::new(10, 0, Currency::Eur);
foo += Money::new(0_50, 2, Currency::Eur);
assert_eq!(foo, Money::new(10_50, 2, Currency::Eur));
ⓘ
foo += Money::new(0_50, 2, Currency::Usd);
source§impl Div for Money
impl Div for Money
source§fn div(self, rhs: Self) -> Self::Output
fn div(self, rhs: Self) -> Self::Output
Panics
- If this currency and the
operand
’s currency are not the same. - When [
Decimal::div
] does.
See also
Examples
use money2::{Currency, Money};
assert_eq!(
Money::new(10, 0, Currency::Eur) / Money::new(2, 0, Currency::Eur),
Money::new(5, 0, Currency::Eur)
);
ⓘ
let _ = Money::new(10, 0, Currency::Eur) / Money::new(2, 0, Currency::Usd);
source§impl DivAssign for Money
impl DivAssign for Money
source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Panics
- When
Money::div
does.
See also
Examples
use money2::{Currency, Money};
let mut foo = Money::new(10, 0, Currency::Eur);
foo /= Money::new(2, 0, Currency::Eur);
assert_eq!(foo, Money::new(5, 0, Currency::Eur));
ⓘ
foo /= Money::new(2, 0, Currency::Usd);
source§impl Exchange for Money
impl Exchange for Money
source§fn exchange_mut(&mut self, currency: Currency, rates: &ExchangeRates)
fn exchange_mut(&mut self, currency: Currency, rates: &ExchangeRates)
source§fn exchange(self, currency: Currency, rates: &ExchangeRates) -> Selfwhere
Self: Sized,
fn exchange(self, currency: Currency, rates: &ExchangeRates) -> Selfwhere Self: Sized,
Exchange some quantity into another
currency
using rates
. Derived from the
exchange_mut
implementation.source§impl Mul for Money
impl Mul for Money
source§fn mul(self, rhs: Self) -> Self::Output
fn mul(self, rhs: Self) -> Self::Output
Panics
- If this currency and the
operand
’s currency are not the same. - When [
Decimal::mul
] does.
See also
Examples
use money2::{Currency, Money};
assert_eq!(
Money::new(10, 0, Currency::Eur) * Money::new(2, 0, Currency::Eur),
Money::new(20, 0, Currency::Eur)
);
ⓘ
let _ = Money::new(10, 0, Currency::Eur) * Money::new(2, 0, Currency::Usd);
source§impl MulAssign for Money
impl MulAssign for Money
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Panics
- When
Money::mul
does.
See also
Examples
use money2::{Currency, Money};
let mut foo = Money::new(10, 0, Currency::Eur);
foo *= Money::new(2, 0, Currency::Eur);
assert_eq!(foo, Money::new(20, 0, Currency::Eur));
ⓘ
foo *= Money::new(2, 0, Currency::Usd);
source§impl Ord for Money
impl Ord for Money
source§impl PartialEq for Money
impl PartialEq for Money
source§impl PartialOrd for Money
impl PartialOrd for Money
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl Rem for Money
impl Rem for Money
source§fn rem(self, rhs: Self) -> Self::Output
fn rem(self, rhs: Self) -> Self::Output
Panics
- If this currency and the
operand
’s currency are not the same. - When [
Decimal::rem
] does.
See also
Examples
use money2::{Currency, Money};
assert_eq!(
Money::new(10, 0, Currency::Eur) % Money::new(3, 0, Currency::Eur),
Money::new(1, 0, Currency::Eur)
);
ⓘ
let _ = Money::new(10, 0, Currency::Eur) % Money::new(3, 0, Currency::Usd);
source§impl RemAssign for Money
impl RemAssign for Money
source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
Panics
- When
Money::rem
does.
See also
Examples
use money2::{Currency, Money};
let mut foo = Money::new(10, 0, Currency::Eur);
foo %= Money::new(3, 0, Currency::Eur);
assert_eq!(foo, Money::new(1, 0, Currency::Eur));
ⓘ
foo %= Money::new(3, 0, Currency::Usd);
source§impl Sub for Money
impl Sub for Money
source§fn sub(self, rhs: Self) -> Self::Output
fn sub(self, rhs: Self) -> Self::Output
Panics
- If this currency and the
operand
’s currency are not the same. - When [
Decimal::sub
] does.
See also
Examples
use money2::{Currency, Money};
assert_eq!(
Money::new(10, 0, Currency::Eur) - Money::new(2, 0, Currency::Eur),
Money::new(8, 0, Currency::Eur)
);
ⓘ
let _ = Money::new(10, 0, Currency::Eur) - Money::new(2, 0, Currency::Usd);
source§impl SubAssign for Money
impl SubAssign for Money
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Panics
- When
Money::sub
does.
See also
Examples
use money2::{Currency, Money};
let mut foo = Money::new(10, 0, Currency::Eur);
foo -= Money::new(2, 0, Currency::Eur);
assert_eq!(foo, Money::new(8, 0, Currency::Eur));
ⓘ
foo -= Money::new(2, 0, Currency::Usd);
impl Copy for Money
impl Eq for Money
impl StructuralEq for Money
impl StructuralPartialEq for Money
Auto Trait Implementations§
impl RefUnwindSafe for Money
impl Send for Money
impl Sync for Money
impl Unpin for Money
impl UnwindSafe for Money
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Checks if this value is equivalent to the given key. Read more
§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.