[][src]Struct monet::Money

pub struct Money {
    pub amount: CurrencyAmount,
    pub currency_code: CurrencyCode,
}

A struct containing an amount of money having a certain currency_code. Note that amount contains fractions of a unit. See AMOUNT_UNIT.

Examples

use monet::{Money, CurrencyAmount, Rates, Operation};
use std::convert::TryInto;

// Custom rates.
let map = vec![("USD", 1_000_000)].into_iter()
    .map(|(code, worth)| (code.try_into().unwrap(), worth.into()))
    .collect();
let rates = Rates::with_rates(map);

let money_owned = Money::with_str_code(CurrencyAmount::with_unit(2), "USD").unwrap();
let money_paid = Money::with_str_code(CurrencyAmount::with_unit(1), "USD").unwrap();

let remaining = (money_owned - money_paid).execute(&rates);

assert_eq!(remaining, Money::with_str_code(CurrencyAmount::with_unit(1), "USD"));

Fields

amount: CurrencyAmountcurrency_code: CurrencyCode

Methods

impl Money[src]

pub fn new(amount: CurrencyAmount, currency_code: CurrencyCode) -> Self[src]

pub fn into_code(self, code: CurrencyCode, rates: &Rates) -> Option<Money>[src]

pub fn with_str_code(
    amount: CurrencyAmount,
    currency_code: &str
) -> Option<Money>
[src]

Creates Money with given amount and code. Returns None if the given code is not three characters long.

Trait Implementations

impl Operation for Money[src]

impl Clone for Money[src]

impl Copy for Money[src]

impl Default for Money[src]

impl Eq for Money[src]

impl PartialEq<Money> for Money[src]

impl Display for Money[src]

Money can be displayed in the following format: 12.10 CHF.

Default precision is dependent on the currency code (see ISO 4217 exponent). A custom precision in range 0..=6 can be provided like this:

use monet::Money;

let money = Money::with_str_code(12_100_000.into(), "CHF").unwrap();

assert_eq!(
    &format!("{}", money),
    "12.10 CHF"
);

assert_eq!(
    &format!("{:.2}", money),
    "12.10 CHF"
);

assert_eq!(
    &format!("{:.6}", money),
    "12.100000 CHF"
);

// Note: the formatted version has lost a decimal due to the
// lower precision
assert_eq!(
    &format!("{:.0}", money),
    "12 CHF"
);

impl Debug for Money[src]

impl Div<Exponent> for Money[src]

type Output = Div<Self>

The resulting type after applying the / operator.

impl<O: Operation> Sub<O> for Money[src]

type Output = Sub<Self, O>

The resulting type after applying the - operator.

impl<O: Operation> Add<O> for Money[src]

type Output = Add<Self, O>

The resulting type after applying the + operator.

impl Mul<Exponent> for Money[src]

type Output = Mul<Self>

The resulting type after applying the * operator.

impl StructuralPartialEq for Money[src]

impl StructuralEq for Money[src]

Auto Trait Implementations

impl Send for Money

impl Sync for Money

impl Unpin for Money

impl UnwindSafe for Money

impl RefUnwindSafe for Money

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]