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

Methods

impl Money
[src]

Creates a Money from its "minor" unit (e.g. US cents to USD).

Creates a Money from its "major" unit (e.g. US dollars to USD).

assert_eq!(Money::of_major_minor(USD, -92_233_720_368_547_758, -08), Money::min(USD));Run

assert_eq!(Money::of_major_minor(USD, 92_233_720_368_547_758, 07), Money::max(USD));Run

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]

Formats the value using the given formatter.

impl PartialEq for Money
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Eq for Money
[src]

impl Copy for Money
[src]

impl Clone for Money
[src]

Returns a copy of the value. Read more

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

Formats the value using the given formatter.

impl FormattableMoney for Money
[src]

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

The resulting type after applying the + operator

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

The resulting type after applying the + operator

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

The resulting type after applying the + operator

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

The resulting type after applying the + operator

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

The resulting type after applying the - operator

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

The resulting type after applying the - operator

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

The resulting type after applying the - operator

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

The resulting type after applying the - operator

The method for the - operator

impl Mul<i64> for Money
[src]

The resulting type after applying the * operator

The method for the * operator

impl<'b> Mul<&'b i64> for Money
[src]

The resulting type after applying the * operator

The method for the * operator

impl<'a> Mul<i64> for &'a Money
[src]

The resulting type after applying the * operator

The method for the * operator

impl<'a, 'b> Mul<&'b i64> for &'a Money
[src]

The resulting type after applying the * operator

The method for the * operator

impl Mul<f64> for Money
[src]

The resulting type after applying the * operator

The method for the * operator

impl<'b> Mul<&'b f64> for Money
[src]

The resulting type after applying the * operator

The method for the * operator

impl<'a> Mul<f64> for &'a Money
[src]

The resulting type after applying the * operator

The method for the * operator

impl<'a, 'b> Mul<&'b f64> for &'a Money
[src]

The resulting type after applying the * operator

The method for the * operator

impl Div<i64> for Money
[src]

The resulting type after applying the / operator

The method for the / operator

impl<'b> Div<&'b i64> for Money
[src]

The resulting type after applying the / operator

The method for the / operator

impl<'a> Div<i64> for &'a Money
[src]

The resulting type after applying the / operator

The method for the / operator

impl<'a, 'b> Div<&'b i64> for &'a Money
[src]

The resulting type after applying the / operator

The method for the / operator

impl Rem<i64> for Money
[src]

The resulting type after applying the % operator

The method for the % operator

impl<'b> Rem<&'b i64> for Money
[src]

The resulting type after applying the % operator

The method for the % operator

impl<'a> Rem<i64> for &'a Money
[src]

The resulting type after applying the % operator

The method for the % operator

impl<'a, 'b> Rem<&'b i64> for &'a Money
[src]

The resulting type after applying the % operator

The method for the % operator

impl Neg for Money
[src]

The resulting type after applying the - operator

The method for the unary - operator

impl<'a> Neg for &'a Money
[src]

The resulting type after applying the - operator

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.

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

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

Performs the conversion.