Struct steel_cent::Money
[−]
[src]
pub struct Money { pub currency: Currency, // some fields omitted }
A signed amount of money in a certain currency.
assert_eq!(16, std::mem::size_of::<Money>());Run
Examples
let price = Money::of_major_minor(USD, 19, 95); let shipping_and_handling = Money::of_major(USD, 10); let convenience_charge = Money::of_major(USD, 6); let discount: f64 = 1.0 - 0.2; // 20% off let discounted_price = price * discount; let fees = shipping_and_handling + convenience_charge; let total = discounted_price + fees; println!("price: {:?}, discounted_price: {:?}", price, discounted_price); assert_eq!(Money::of_minor(USD, 1596), discounted_price); assert_eq!(Money::of_minor(USD, 3196), total); assert_eq!((price * discount) + shipping_and_handling + convenience_charge, total);Run
Fields
currency: Currency
Methods
impl Money
[src]
fn of_minor(currency: Currency, amount_minor: i64) -> Self
Creates a Money from its "minor" unit (e.g. US cents to USD).
fn of_major(currency: Currency, amount_major: i64) -> Self
Creates a Money from its "major" unit (e.g. US dollars to USD).
fn of_major_minor(currency: Currency, amount_major: i64, amount_minor: i64) -> Self
fn major_part(&self) -> i64
fn minor_part(&self) -> i64
fn min(currency: Currency) -> Money
assert_eq!(Money::of_major_minor(USD, -92_233_720_368_547_758, -08), Money::min(USD));Run
fn max(currency: Currency) -> Money
assert_eq!(Money::of_major_minor(USD, 92_233_720_368_547_758, 07), Money::max(USD));Run
fn convert_to(&self, currency: Currency, conversion_multiplier: f64) -> Money
let five_usd = Money::of_major(USD, 5); let one_cent = Money::of_minor(USD, 1); let three_pound_seventy_five = Money::of_major_minor(GBP, 3, 75); let gbp_per_usd = 0.75; let five_ten_yen = Money::of_major(JPY, 510); let jpy_per_usd = 102.15; assert_eq!(three_pound_seventy_five, five_usd.convert_to(GBP, gbp_per_usd)); assert_eq!(five_usd, three_pound_seventy_five.convert_to(USD, gbp_per_usd.recip())); assert_eq!(five_ten_yen, five_usd.convert_to(JPY, jpy_per_usd)); // truncated assert_eq!(five_usd - one_cent, five_ten_yen.convert_to(USD, jpy_per_usd.recip()));Run
Trait Implementations
impl Debug for Money
[src]
impl PartialEq for Money
[src]
fn eq(&self, __arg_0: &Money) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Money) -> bool
This method tests for !=
.
impl Eq for Money
[src]
impl Copy for Money
[src]
impl Clone for Money
[src]
fn clone(&self) -> Money
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl Display for Money
[src]
Displays the value with formatting::STYLE_GENERIC
. See the formatting
module for other
pre-defined and custom styles.
assert_eq!("1,234.56\u{a0}GBP", format!("{}", &Money::of_minor(GBP, 123456)));Run
impl FormattableMoney for Money
[src]
fn unformatted_minor_amount(&self) -> String
fn currency(&self) -> Currency
impl Add for Money
[src]
let one = Money::of_major(USD, 1); assert_eq!(Money::of_major(USD, 2), one.clone() + one);Run
Attempting to add moneys of different currencies will panic.
Money::of_major(USD, 1) + Money::of_major(JPY, 1); // panics!Run
type Output = Money
The resulting type after applying the +
operator
fn add(self, other: Money) -> Money
The method for the +
operator
impl<'b> Add<&'b Money> for Money
[src]
let one = Money::of_major(USD, 1); assert_eq!(Money::of_major(USD, 2), one.clone() + &one);Run
Attempting to add moneys of different currencies will panic.
Money::of_major(USD, 1) + &Money::of_major(JPY, 1); // panics!Run
type Output = Money
The resulting type after applying the +
operator
fn add(self, other: &'b Money) -> Money
The method for the +
operator
impl<'a> Add<Money> for &'a Money
[src]
let one = Money::of_major(USD, 1); assert_eq!(Money::of_major(USD, 2), &one + one.clone());Run
Attempting to add moneys of different currencies will panic.
&Money::of_major(USD, 1) + Money::of_major(JPY, 1); // panics!Run
type Output = Money
The resulting type after applying the +
operator
fn add(self, other: Money) -> Money
The method for the +
operator
impl<'a, 'b> Add<&'b Money> for &'a Money
[src]
let one = Money::of_major(USD, 1); assert_eq!(Money::of_major(USD, 2), &one + &one);Run
Attempting to add moneys of different currencies will panic.
&Money::of_major(USD, 1) + &Money::of_major(JPY, 1); // panics!Run
type Output = Money
The resulting type after applying the +
operator
fn add(self, other: &'b Money) -> Money
The method for the +
operator
impl Sub for Money
[src]
let one = Money::of_major(USD, 1); assert_eq!(Money::of_major(USD, 0), one.clone() - one);Run
Attempting to subtract moneys of different currencies will panic.
Money::of_major(USD, 1) - Money::of_major(JPY, 1); // panics!Run
type Output = Money
The resulting type after applying the -
operator
fn sub(self, other: Money) -> Money
The method for the -
operator
impl<'b> Sub<&'b Money> for Money
[src]
let one = Money::of_major(USD, 1); assert_eq!(Money::of_major(USD, 0), one.clone() - &one);Run
Attempting to subtract moneys of different currencies will panic.
Money::of_major(USD, 1) - &Money::of_major(JPY, 1); // panics!Run
type Output = Money
The resulting type after applying the -
operator
fn sub(self, other: &'b Money) -> Money
The method for the -
operator
impl<'a> Sub<Money> for &'a Money
[src]
let one = Money::of_major(USD, 1); assert_eq!(Money::of_major(USD, 0), &one - one.clone());Run
Attempting to subtract moneys of different currencies will panic.
&Money::of_major(USD, 1) - Money::of_major(JPY, 1); // panics!Run
type Output = Money
The resulting type after applying the -
operator
fn sub(self, other: Money) -> Money
The method for the -
operator
impl<'a, 'b> Sub<&'b Money> for &'a Money
[src]
let one = Money::of_major(USD, 1); assert_eq!(Money::of_major(USD, 0), &one - &one);Run
Attempting to subtract moneys of different currencies will panic.
&Money::of_major(USD, 1) - &Money::of_major(JPY, 1); // panics!Run
type Output = Money
The resulting type after applying the -
operator
fn sub(self, other: &Money) -> Money
The method for the -
operator
impl Mul<i64> for Money
[src]
type Output = Money
The resulting type after applying the *
operator
fn mul(self, n: i64) -> Money
The method for the *
operator
impl<'b> Mul<&'b i64> for Money
[src]
type Output = Money
The resulting type after applying the *
operator
fn mul(self, n: &i64) -> Money
The method for the *
operator
impl<'a> Mul<i64> for &'a Money
[src]
type Output = Money
The resulting type after applying the *
operator
fn mul(self, n: i64) -> Money
The method for the *
operator
impl<'a, 'b> Mul<&'b i64> for &'a Money
[src]
type Output = Money
The resulting type after applying the *
operator
fn mul(self, n: &i64) -> Money
The method for the *
operator
impl Mul<f64> for Money
[src]
type Output = Money
The resulting type after applying the *
operator
fn mul(self, n: f64) -> Money
The method for the *
operator
impl<'b> Mul<&'b f64> for Money
[src]
type Output = Money
The resulting type after applying the *
operator
fn mul(self, n: &f64) -> Money
The method for the *
operator
impl<'a> Mul<f64> for &'a Money
[src]
type Output = Money
The resulting type after applying the *
operator
fn mul(self, n: f64) -> Money
The method for the *
operator
impl<'a, 'b> Mul<&'b f64> for &'a Money
[src]
type Output = Money
The resulting type after applying the *
operator
fn mul(self, n: &f64) -> Money
The method for the *
operator
impl Div<i64> for Money
[src]
type Output = Money
The resulting type after applying the /
operator
fn div(self, n: i64) -> Money
The method for the /
operator
impl<'b> Div<&'b i64> for Money
[src]
type Output = Money
The resulting type after applying the /
operator
fn div(self, n: &i64) -> Money
The method for the /
operator
impl<'a> Div<i64> for &'a Money
[src]
type Output = Money
The resulting type after applying the /
operator
fn div(self, n: i64) -> Money
The method for the /
operator
impl<'a, 'b> Div<&'b i64> for &'a Money
[src]
type Output = Money
The resulting type after applying the /
operator
fn div(self, n: &i64) -> Money
The method for the /
operator
impl Rem<i64> for Money
[src]
type Output = Money
The resulting type after applying the %
operator
fn rem(self, n: i64) -> Money
The method for the %
operator
impl<'b> Rem<&'b i64> for Money
[src]
type Output = Money
The resulting type after applying the %
operator
fn rem(self, n: &i64) -> Money
The method for the %
operator
impl<'a> Rem<i64> for &'a Money
[src]
type Output = Money
The resulting type after applying the %
operator
fn rem(self, n: i64) -> Money
The method for the %
operator
impl<'a, 'b> Rem<&'b i64> for &'a Money
[src]
type Output = Money
The resulting type after applying the %
operator
fn rem(self, n: &i64) -> Money
The method for the %
operator
impl Neg for Money
[src]
type Output = Money
The resulting type after applying the -
operator
fn neg(self) -> Money
The method for the unary -
operator
impl<'a> Neg for &'a Money
[src]
type Output = Money
The resulting type after applying the -
operator
fn neg(self) -> Money
The method for the unary -
operator
impl PartialOrd for Money
[src]
Compares to Money
of the same currency only.
Returns None
when currencies differ, causing all comparison operators to return false when
currencies differ.
fn partial_cmp(&self, other: &Money) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl From<SmallMoney> for Money
[src]
Converts to Money
.
assert_eq!(Money::of_major(USD, 2), Money::from(SmallMoney::of_major(USD, 2)));Run
fn from(small: SmallMoney) -> Self
Performs the conversion.