Struct steel_cent::Money [] [src]

pub struct Money {
    pub currency: Currency,
    // some fields omitted
}

A signed amount of money in a certain currency, with the currency's standard number of decimal places.

Note that the arithmetic ops implementations all delegate to the checked_ methods, panicking on None, even in a release build. (This is in contrast to primitive ops, which will overflow in a release build.)

A Money is 128 bits in size.

assert_eq!(16, std::mem::size_of::<Money>());

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);

Fields

Methods

impl Money
[src]

[src]

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

[src]

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

[src]

[src]

[src]

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

[src]

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

[src]

Returns the major unit part of the amount.

assert_eq!(2, Money::of_minor(USD, 2_99).major_part());

[src]

Returns the minor unit part of the amount.

assert_eq!(99, Money::of_minor(USD, 2_99).minor_part());

[src]

Returns the total amount in the currency's minor unit.

let two_dollars = Money::of_major(USD, 2);
assert_eq!(200, two_dollars.minor_amount());

[src]

let five_usd = Money::of_major(USD, 5);
let three_pound_seventy_five = Money::of_major_minor(GBP, 3, 75);
let gbp_per_usd = 0.75;
let five_eleven_yen = Money::of_major(JPY, 511);
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!(510.75, 5.0 * jpy_per_usd); // but JPY has zero decimal places.
assert_eq!(five_eleven_yen, five_usd.convert_to(JPY, jpy_per_usd)); // rounded.
assert_eq!(five_usd, five_eleven_yen.convert_to(USD, jpy_per_usd.recip())); // rounded.

[src]

Returns absolute value, except for the minimum value, which cannot be negated.

let c = Money::of_major(USD, 100);
assert_eq!(Some(c), c.checked_abs());
assert_eq!(Some(c), (-c).checked_abs());
assert_eq!(None, Money::min(USD).checked_abs());

[src]

Returns absolute value, except for the minimum value, which cannot be negated.

let c = Money::of_major(USD, 100);
assert_eq!(c, c.abs());
assert_eq!(c, (-c).abs());

Panics

Panics for the minimum value.

[src]

assert_eq!(Some(Money::of_minor(USD, 4_01)),
           Money::of_major(USD, 4).checked_add(Money::of_minor(USD, 1)));
assert_eq!(None, Money::max(USD).checked_add(Money::of_minor(USD, 1)));

Panics

Panics when currencies differ.

[src]

assert_eq!(Some(Money::of_minor(USD, 3_99)),
           Money::of_major(USD, 4).checked_sub(Money::of_minor(USD, 1)));
assert_eq!(None, Money::min(USD).checked_sub(Money::of_minor(USD, 1)));

Panics

Panics when currencies differ.

[src]

assert_eq!(Some(Money::of_major(USD, 8)), Money::of_major(USD, 4).checked_mul(2));
assert_eq!(None, Money::max(USD).checked_mul(2));
assert_eq!(None, Money::min(USD).checked_mul(-1));

[src]

Checked multiplication by a float with rounding. Note that a float has less-than-integer precision for very large and very small amounts of money, which can result in surprising rounding errors.

assert_eq!(Some(Money::of_minor(USD, 8_40)), Money::of_major(USD, 4).checked_mul_f(2.1));
assert_eq!(None, Money::max(USD).checked_mul_f(1.01));
assert_eq!(None, Money::min(USD).checked_mul_f(-1.0));

[src]

assert_eq!(Some(Money::of_major(USD, 2)), Money::of_minor(USD, 4_01).checked_div(2));
assert_eq!(None, Money::of_major(USD, 1).checked_div(0));
assert_eq!(None, Money::min(USD).checked_div(-1));

[src]

assert_eq!(Some(Money::of_minor(USD, 1)), Money::of_minor(USD, 4_01).checked_rem(2));
assert_eq!(None, Money::of_major(USD, 1).checked_rem(0));
assert_eq!(None, Money::min(USD).checked_rem(-1));

[src]

Negates the value, except for the minimum value, which cannot be negated.

assert_eq!(Some(Money::of_major(USD, -1)), Money::of_major(USD, 1).checked_neg());
assert_eq!(None, Money::min(USD).checked_neg());

[src]

assert_eq!(Money::of_minor(USD, 4_01),
           Money::of_major(USD, 4).saturating_add(Money::of_minor(USD, 1)));
assert_eq!(Money::max(USD),
           Money::max(USD).saturating_add(Money::of_minor(USD, 1)));

Panics

Panics when currencies differ.

[src]

assert_eq!(Money::of_minor(USD, 3_99),
           Money::of_major(USD, 4).saturating_sub(Money::of_minor(USD, 1)));
assert_eq!(Money::min(USD),
           Money::min(USD).saturating_sub(Money::of_minor(USD, 1)));

Panics

Panics when currencies differ.

[src]

assert_eq!(Money::of_major(USD, 8), Money::of_major(USD, 4).saturating_mul(2));
assert_eq!(Money::max(USD), Money::max(USD).saturating_mul(2));
assert_eq!(Money::max(USD), Money::min(USD).saturating_mul(-1));

Trait Implementations

impl Debug for Money
[src]

[src]

Formats the value using the given formatter.

impl PartialEq for Money
[src]

[src]

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

[src]

This method tests for !=.

impl Eq for Money
[src]

impl Copy for Money
[src]

impl Clone for Money
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl Display for Money
[src]

[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)));

impl FormattableMoney for Money
[src]

impl ParseableMoney for Money
[src]

[src]

impl Add for Money
[src]

The resulting type after applying the + operator.

[src]

Adds two Moneys.

Examples

let one = Money::of_major(USD, 1);
assert_eq!(Money::of_major(USD, 2), one + one);
// Likewise for refs:
let one = Money::of_major(USD, 1);
assert_eq!(Money::of_major(USD, 2), one + &one);
assert_eq!(Money::of_major(USD, 2), &one + one);
assert_eq!(Money::of_major(USD, 2), &one + &one);

Panics

Panics when currencies differ.

Money::of_major(USD, 1) + Money::of_major(JPY, 1); // panics!

Panics when addition of minor amounts would overflow.

Money::of_minor(USD, std::i64::MAX) + Money::of_minor(USD, 1); // panics!

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

The resulting type after applying the + operator.

[src]

Performs the + operation.

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

The resulting type after applying the + operator.

[src]

Performs the + operation.

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

The resulting type after applying the + operator.

[src]

Performs the + operation.

impl Sub for Money
[src]

The resulting type after applying the - operator.

[src]

Subtracts one Money from another.

Examples

let one = Money::of_major(USD, 1);
assert_eq!(Money::of_major(USD, 0), one - one);
// Likewise for refs:
assert_eq!(Money::of_major(USD, 0), one - &one);
assert_eq!(Money::of_major(USD, 0), &one - one);
assert_eq!(Money::of_major(USD, 0), &one - &one);

Panics

Panics when currencies differ.

Money::of_major(USD, 1) - Money::of_major(JPY, 1); // panics!

Panics when subtraction of minor amounts would overflow.

Money::of_minor(USD, std::i64::MIN) - Money::of_minor(USD, 1); // panics!

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

The resulting type after applying the - operator.

[src]

Performs the - operation.

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

The resulting type after applying the - operator.

[src]

Performs the - operation.

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

The resulting type after applying the - operator.

[src]

Performs the - operation.

impl Mul<i64> for Money
[src]

The resulting type after applying the * operator.

[src]

Multiplies money by an integer.

Examples

let two_usd = Money::of_major(USD, 2);
let four_usd = Money::of_major(USD, 4);
assert_eq!(four_usd, two_usd * 2);
// Likewise for refs:
assert_eq!(four_usd, two_usd * &2);
assert_eq!(four_usd, &two_usd * 2);
assert_eq!(four_usd, &two_usd * &2);

Panics

Panics when multiplication of minor amount would overflow.

Money::of_minor(USD, std::i64::MAX) * 2; // panics!

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

The resulting type after applying the * operator.

[src]

Performs the * operation.

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

The resulting type after applying the * operator.

[src]

Performs the * operation.

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

The resulting type after applying the * operator.

[src]

Performs the * operation.

impl Mul<f64> for Money
[src]

The resulting type after applying the * operator.

[src]

Multiplies money by a float, rounding if needed.

Examples

let one = Money::of_major(USD, 1);
let one01 = Money::of_minor(USD, 1_01);
assert_eq!(one01, one * 1.005001);
assert_eq!(Money::of_minor(USD, 1_005_00), Money::of_major(USD, 1_000) * 1.005001);
assert_eq!(Money::of_minor(USD, 10_050_01), Money::of_major(USD, 10_000) * 1.005001);
// Likewise for refs:
assert_eq!(one01, one * &1.005001);
assert_eq!(one01, &one * 1.005001);
assert_eq!(one01, &one * &1.005001);

Panics

Panics when multiplication of minor amount would overflow.

Money::of_minor(USD, std::i64::MAX) * 1.01; // panics!

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

The resulting type after applying the * operator.

[src]

Performs the * operation.

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

The resulting type after applying the * operator.

[src]

Performs the * operation.

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

The resulting type after applying the * operator.

[src]

Performs the * operation.

impl Div<i64> for Money
[src]

The resulting type after applying the / operator.

[src]

Divides money by an integer.

Examples

let two_usd = Money::of_major(USD, 2);
let four01 = Money::of_minor(USD, 4_01);
assert_eq!(two_usd, four01 / 2);
// Likewise for refs:
assert_eq!(two_usd, &four01 / 2);
assert_eq!(two_usd, four01 / &2);
assert_eq!(two_usd, &four01 / &2);

Panics

Panics when division of minor amount would overflow.

Money::of_minor(USD, std::i64::MIN) / -1; // panics!

Panics when n is zero.

Money::of_minor(USD, 1) / 0; // panics!

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

The resulting type after applying the / operator.

[src]

Performs the / operation.

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

The resulting type after applying the / operator.

[src]

Performs the / operation.

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

The resulting type after applying the / operator.

[src]

Performs the / operation.

impl Rem<i64> for Money
[src]

The resulting type after applying the % operator.

[src]

Remainder of dividing money by an integer.

Examples

let one_cent = Money::of_minor(USD, 1);
let four01 = Money::of_minor(USD, 4_01);
assert_eq!(one_cent, four01 % 2);
// Likewise for refs:
assert_eq!(one_cent, &four01 % 2);
assert_eq!(one_cent, four01 % &2);
assert_eq!(one_cent, &four01 % &2);

Panics

Panics when division of minor amount would overflow.

Money::of_minor(USD, std::i64::MIN) % -1; // panics!

Panics when n is zero.

Money::of_minor(USD, 1) % 0; // panics!

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

The resulting type after applying the % operator.

[src]

Performs the % operation.

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

The resulting type after applying the % operator.

[src]

Performs the % operation.

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

The resulting type after applying the % operator.

[src]

Performs the % operation.

impl Neg for Money
[src]

The resulting type after applying the - operator.

[src]

Negation.

Examples

assert_eq!(Money::of_minor(USD, -1), -Money::of_minor(USD, 1));

Panics

Panics when negation would overflow, which is only the case for the minimum value.

-Money::min(USD); // panics!

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

The resulting type after applying the - operator.

[src]

Negation of ref.

Examples

assert_eq!(Money::of_minor(USD, -1), -(&Money::of_minor(USD, 1)));

Panics

Panics when negation would overflow.

-(&Money::of_minor(USD, std::i64::MIN)); // panics!

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.

[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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 a SmallMoney to a Money.

assert_eq!(Money::of_major(USD, 2),
           Money::from(SmallMoney::of_major(USD, 2)));

[src]

Performs the conversion.