Skip to content

Commit

Permalink
Merge pull request #32 from sjoerdsimons/wip/sjoerd/decimal
Browse files Browse the repository at this point in the history
Implement multiple/division with decimals
  • Loading branch information
varunsrin authored Jul 13, 2020
2 parents 709362b + 7e8d602 commit 5333da1
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions src/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,22 @@ macro_rules! impl_mul_div {
type Output = Money;

fn mul(self, rhs: $type) -> Money {
let rhs = Decimal::from_str(&rhs.to_string()).unwrap();
Money::from_decimal(self.amount * rhs, self.currency)
Money::from_decimal(self.amount * Decimal::from(rhs), self.currency)
}
}

impl Mul<Money> for $type {
type Output = Money;

fn mul(self, rhs: Money) -> Money {
let lhs = Decimal::from_str(&self.to_string()).unwrap();
Money::from_decimal(rhs.amount * lhs, rhs.currency)
Money::from_decimal(rhs.amount * Decimal::from(self), rhs.currency)
}
}

impl MulAssign<$type> for Money {
fn mul_assign(&mut self, rhs: $type) {
let rhs = Decimal::from_str(&rhs.to_string()).unwrap();
*self = Self {
amount: self.amount * rhs,
amount: self.amount * Decimal::from(rhs),
currency: self.currency,
};
}
Expand All @@ -108,25 +105,22 @@ macro_rules! impl_mul_div {
type Output = Money;

fn div(self, rhs: $type) -> Money {
let rhs = Decimal::from_str(&rhs.to_string()).unwrap();
Money::from_decimal(self.amount / rhs, self.currency)
Money::from_decimal(self.amount / Decimal::from(rhs), self.currency)
}
}

impl Div<Money> for $type {
type Output = Money;

fn div(self, rhs: Money) -> Money {
let lhs = Decimal::from_str(&self.to_string()).unwrap();
Money::from_decimal(lhs / rhs.amount, rhs.currency)
Money::from_decimal(Decimal::from(self) / rhs.amount, rhs.currency)
}
}

impl DivAssign<$type> for Money {
fn div_assign(&mut self, rhs: $type) {
let rhs = Decimal::from_str(&rhs.to_string()).unwrap();
*self = Self {
amount: self.amount / rhs,
amount: self.amount / Decimal::from(rhs),
currency: self.currency,
};
}
Expand All @@ -144,6 +138,7 @@ impl_mul_div!(u8);
impl_mul_div!(u16);
impl_mul_div!(u32);
impl_mul_div!(u64);
impl_mul_div!(Decimal);

impl PartialOrd for Money {
fn partial_cmp(&self, other: &Money) -> Option<Ordering> {
Expand Down Expand Up @@ -517,26 +512,58 @@ mod tests {

#[test]
fn money_multiplication_and_division() {
// Multiplication
// Multiplication integer
assert_eq!(money!(2, "USD"), money!(1, "USD") * 2);
assert_eq!(money!(2, "USD"), money!(-1, "USD") * -2);
assert_eq!(money!(2, "USD"), -2 * money!(-1, "USD"));

// Division
// Multiplication decimal
assert_eq!(money!(2, "USD"), money!(1, "USD") * Decimal::new(2, 0));
assert_eq!(money!(2, "USD"), money!(-1, "USD") * Decimal::new(-2, 0));
assert_eq!(money!(2, "USD"), Decimal::new(-2, 0) * money!(-1, "USD"));
assert_eq!(money!(2, "USD"), money!(4, "USD") * Decimal::new(5, 1));

// Division integer
assert_eq!(money!(2, "USD"), money!(4, "USD") / 2);
assert_eq!(money!(2, "USD"), money!(-4, "USD") / -2);
assert_eq!(money!("0.5", "USD"), -1 / money!(-2, "USD"));
assert_eq!(money!("2.0", "USD"), money!(-2, "USD") / -1);

//MulAssign
// Division decimal
assert_eq!(money!(2, "USD"), money!(4, "USD") / Decimal::new(2, 0));
assert_eq!(money!(2, "USD"), money!(-4, "USD") / Decimal::new(-2, 0));
assert_eq!(
money!("0.5", "USD"),
Decimal::new(-1, 0) / money!(-2, "USD")
);
assert_eq!(
money!("2.0", "USD"),
money!(-2, "USD") / Decimal::new(-1, 0)
);
assert_eq!(
money!("4.0", "USD"),
money!(-2, "USD") / Decimal::new(-5, 1)
);

//MulAssign integer
let mut money = money!(1, "USD");
money *= 2;
assert_eq!(money!(2, "USD"), money);

//DivAssign
//MulAssign decimal
let mut money = money!(1, "USD");
money *= Decimal::new(2, 0);
assert_eq!(money!(2, "USD"), money);

//DivAssign integer
let mut money = money!(1, "USD");
money /= -2;
assert_eq!(money!("-0.5", "USD"), money);

//DivAssign decimal
let mut money = money!(1, "USD");
money /= Decimal::new(-2, 0);
assert_eq!(money!("-0.5", "USD"), money);
}

#[test]
Expand Down

0 comments on commit 5333da1

Please sign in to comment.