pub struct Money { /* private fields */ }Expand description
An amount in one currency, counted in that currency’s minor unit.
Implementations§
Source§impl Money
impl Money
Sourcepub const fn from_minor_units(minor_units: i64, currency: Currency) -> Self
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.
Sourcepub fn parse(value: &str, currency: Currency) -> Result<Self, MoneyError>
pub fn parse(value: &str, currency: Currency) -> Result<Self, MoneyError>
Reads a plain decimal string such as "10.50".
Sourcepub const fn minor_units(self) -> i64
pub const fn minor_units(self) -> i64
The amount as a count of minor units.
Sourcepub fn require_positive(self) -> Result<Self, MoneyError>
pub fn require_positive(self) -> Result<Self, MoneyError>
Fails unless the amount is greater than zero.
Sourcepub fn checked_add(self, other: Self) -> Result<Self, MoneyError>
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.
Sourcepub fn checked_sub(self, other: Self) -> Result<Self, MoneyError>
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.
Sourcepub fn checked_mul(self, count: u32) -> Result<Self, MoneyError>
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.
Sourcepub fn to_decimal_string(self) -> String
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§
impl Copy for Money
impl Eq for Money
Source§impl PartialOrd for Money
Orders two amounts, and refuses to order two currencies.
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.