diff --git a/sqlx-core/src/postgres/types/money.rs b/sqlx-core/src/postgres/types/money.rs index 50e026ea..7d2f3fda 100644 --- a/sqlx-core/src/postgres/types/money.rs +++ b/sqlx-core/src/postgres/types/money.rs @@ -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 for PgMoney { @@ -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) + ); + } }