Skip to content

Commit

Permalink
Add money conversions to Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Jul 3, 2020
1 parent 48e7c00 commit fbd0360
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions sqlx-core/src/postgres/types/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ impl PgMoney {

bigdecimal::BigDecimal::new(digits, scale)
}

/// Convert the money value into a [`Decimal`] using the correct precision
/// defined in the PostgreSQL settings. The default precision is two.
///
/// [`Decimal`]: ../../types/struct.BigDecimal.html
#[cfg(feature = "decimal")]
pub fn to_decimal(self, scale: u32) -> rust_decimal::Decimal {
rust_decimal::Decimal::new(self.0, scale)
}
}

impl Type<Postgres> for PgMoney {
Expand Down Expand Up @@ -214,4 +223,13 @@ mod tests {
money.to_bigdecimal(2)
);
}

#[test]
#[cfg(feature = "decimal")]
fn conversion_to_decimal_works() {
assert_eq!(
rust_decimal::Decimal::new(12345, 2),
PgMoney(12345).to_decimal(2)
);
}
}

0 comments on commit fbd0360

Please sign in to comment.