Struct Money

Source
pub struct Money<'a, T: FormattableCurrency> { /* private fields */ }
Expand description

Represents an amount of a given currency.

Money represents financial amounts through a Decimal (owned) and a Currency (refernce). Operations on Money objects always create new instances of Money, with the exception of round().

Implementations§

Source§

impl<'a, T: FormattableCurrency> Money<'a, T>

Source

pub fn from_str( amount: &str, currency: &'a T, ) -> Result<Money<'a, T>, MoneyError>

Creates a Money object given an amount string and a currency str.

Supports fuzzy amount strings like “100”, “100.00” and “-100.00”

Source

pub fn from_minor(amount: i64, currency: &'a T) -> Money<'a, T>

Creates a Money object given an integer and a currency reference.

The integer represents minor units of the currency (e.g. 1000 -> 10.00 in USD )

Source

pub fn from_major(amount: i64, currency: &'a T) -> Money<'a, T>

Creates a Money object given an integer and a currency reference.

The integer represents major units of the currency (e.g. 1000 -> 1,000 in USD )

Source

pub fn from_decimal(amount: Decimal, currency: &'a T) -> Money<'a, T>

Creates a Money object given a decimal amount and a currency reference.

Source

pub fn amount(&self) -> &Decimal

Returns a reference to the Decimal amount.

Source

pub fn currency(&self) -> &'a T

Returns the Currency type.

Source

pub fn is_zero(&self) -> bool

Returns true if amount == 0.

Source

pub fn is_positive(&self) -> bool

Returns true if amount > 0.

Source

pub fn is_negative(&self) -> bool

Returns true if amount < 0.

Source

pub fn allocate_to(&self, number: i32) -> Result<Vec<Money<'a, T>>, MoneyError>

Divides money equally into n shares.

If the divison cannot be applied perfectly, it allocates the remainder to some of the shares.

Source

pub fn allocate( &self, ratios: Vec<i32>, ) -> Result<Vec<Money<'a, T>>, MoneyError>

Divides money into n shares according to a particular ratio.

If the divison cannot be applied perfectly, it allocates the remainder to some of the shares.

Source

pub fn round(&self, digits: u32, strategy: Round) -> Money<'a, T>

Returns a Money rounded to the specified number of minor units using the rounding strategy.

Trait Implementations§

Source§

impl<'a, T: FormattableCurrency> Add for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Money<'a, T>) -> Money<'a, T>

Performs the + operation. Read more
Source§

impl<'a, T: FormattableCurrency> AddAssign for Money<'a, T>

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl<'a, T: Clone + FormattableCurrency> Clone for Money<'a, T>

Source§

fn clone(&self) -> Money<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T: Debug + FormattableCurrency> Debug for Money<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: FormattableCurrency> Display for Money<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Decimal> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Decimal) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for Decimal

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for i16

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for i32

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for i64

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for i8

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for isize

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for u16

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for u32

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for u64

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for u8

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<Money<'a, T>> for usize

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<i16> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i16) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<i32> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i32) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<i64> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i64) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<i8> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i8) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<isize> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: isize) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<u16> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u16) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<u32> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u32) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<u64> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u64) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<u8> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u8) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> Div<usize> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: usize) -> Money<'a, T>

Performs the / operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<Decimal> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: Decimal)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<i16> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: i16)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<i32> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: i32)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<i64> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: i64)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<i8> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: i8)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<isize> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: isize)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<u16> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: u16)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<u32> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: u32)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<u64> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: u64)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<u8> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: u8)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> DivAssign<usize> for Money<'a, T>

Source§

fn div_assign(&mut self, rhs: usize)

Performs the /= operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Decimal> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Decimal) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for Decimal

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for i16

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for i32

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for i64

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for i8

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for isize

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for u16

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for u32

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for u64

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for u8

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<Money<'a, T>> for usize

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money<'a, T>) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<i16> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i16) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<i32> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i32) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<i64> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i64) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<i8> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i8) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<isize> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: isize) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<u16> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u16) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<u32> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u32) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<u64> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u64) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<u8> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u8) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> Mul<usize> for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: usize) -> Money<'a, T>

Performs the * operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<Decimal> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: Decimal)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<i16> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: i16)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<i32> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: i32)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<i64> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: i64)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<i8> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: i8)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<isize> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: isize)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<u16> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: u16)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<u32> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: u32)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<u64> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: u64)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<u8> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: u8)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> MulAssign<usize> for Money<'a, T>

Source§

fn mul_assign(&mut self, rhs: usize)

Performs the *= operation. Read more
Source§

impl<'a, T: FormattableCurrency> Ord for Money<'a, T>

Source§

fn cmp(&self, other: &Money<'a, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a, T: PartialEq + FormattableCurrency> PartialEq for Money<'a, T>

Source§

fn eq(&self, other: &Money<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T: FormattableCurrency> PartialOrd for Money<'a, T>

Source§

fn partial_cmp(&self, other: &Money<'a, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T: FormattableCurrency> Sub for Money<'a, T>

Source§

type Output = Money<'a, T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Money<'a, T>) -> Money<'a, T>

Performs the - operation. Read more
Source§

impl<'a, T: FormattableCurrency> SubAssign for Money<'a, T>

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl<'a, T: Eq + FormattableCurrency> Eq for Money<'a, T>

Source§

impl<'a, T: FormattableCurrency> StructuralPartialEq for Money<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for Money<'a, T>

§

impl<'a, T> RefUnwindSafe for Money<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Money<'a, T>
where T: Sync,

§

impl<'a, T> Sync for Money<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Money<'a, T>

§

impl<'a, T> UnwindSafe for Money<'a, T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.