Skip to main content

Money

Struct Money 

Source
pub struct Money { /* private fields */ }
Expand description

An amount in one currency, counted in that currency’s minor unit.

Implementations§

Source§

impl Money

Source

pub const fn from_minor_units(minor_units: i64, currency: Currency) -> Self

Builds an amount from a count of minor units — 1050 for 10.50 TRY.

Source

pub fn parse(value: &str, currency: Currency) -> Result<Self, MoneyError>

Reads a plain decimal string such as "10.50".

Source

pub const fn minor_units(self) -> i64

The amount as a count of minor units.

Source

pub const fn currency(self) -> Currency

The currency the amount is in.

Source

pub fn require_positive(self) -> Result<Self, MoneyError>

Fails unless the amount is greater than zero.

Source

pub fn checked_add(self, other: Self) -> Result<Self, MoneyError>

Adds another amount in the same currency.

Fails on a currency mismatch, and on the overflow that would otherwise wrap a total round to a negative one.

Source

pub fn checked_sub(self, other: Self) -> Result<Self, MoneyError>

Subtracts another amount in the same currency.

The result may be negative, because a ledger needs it to be: an over-refund is a number somebody has to see, not one to clamp away. Money::require_positive is what refuses it where it must be refused.

Source

pub fn checked_mul(self, count: u32) -> Result<Self, MoneyError>

Multiplies by a count — a unit price by how many were bought.

Fails on the overflow that would otherwise wrap a line total round to a negative one.

Source

pub const fn is_zero(self) -> bool

Whether the amount is exactly zero.

Source

pub fn to_decimal_string(self) -> String

Renders the amount as a plain decimal string, without the currency code.

This is the form providers that take a decimal amount expect: 10.50, never 10.5 and never 1.05e1.

Trait Implementations§

Source§

impl Clone for Money

Source§

fn clone(&self) -> Money

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Money

Source§

impl Debug for Money

Source§

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

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

impl Display for Money

Source§

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

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

impl Eq for Money

Source§

impl PartialEq for Money

Source§

fn eq(&self, other: &Money) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Money

Orders two amounts, and refuses to order two currencies.

partial_cmp answers None across currencies, because ten lira and ten dollars have no order. Deriving Ord would invent one out of the declaration order of Currency, which is how a comparison quietly starts answering a question nobody asked.

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 StructuralPartialEq for Money

Auto Trait Implementations§

§

impl Freeze for Money

§

impl RefUnwindSafe for Money

§

impl Send for Money

§

impl Sync for Money

§

impl Unpin for Money

§

impl UnsafeUnpin for Money

§

impl UnwindSafe for Money

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.