Add money conversions to Decimal

This commit is contained in:
Julius de Bruijn
2020-07-03 14:33:33 +02:00
committed by Austin Bonander
parent fd837fce09
commit 95ac38caed

View File

@@ -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 {
@@ -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)
);
}
}