mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-23 10:38:57 +00:00
Add money conversions to Decimal
This commit is contained in:
committed by
Austin Bonander
parent
fd837fce09
commit
95ac38caed
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user