mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-30 15:15:01 +00:00
postgres: fix decimal conversions
This commit is contained in:
@@ -115,9 +115,11 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric {
|
||||
let mut digits = Vec::with_capacity(digits_len);
|
||||
|
||||
if let Some(first) = base_10.get(..offset) {
|
||||
if offset != 0 {
|
||||
if !first.is_empty() {
|
||||
digits.push(base_10_to_10000(first));
|
||||
}
|
||||
} else if offset != 0 {
|
||||
digits.push(base_10_to_10000(&base_10) * 10i16.pow(3 - base_10.len() as u32));
|
||||
}
|
||||
|
||||
if let Some(rest) = base_10.get(offset..) {
|
||||
@@ -275,6 +277,34 @@ mod bigdecimal_to_pgnumeric {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_hundredth() {
|
||||
let one_hundredth: BigDecimal = "0.01".parse().unwrap();
|
||||
assert_eq!(
|
||||
PgNumeric::try_from(&one_hundredth).unwrap(),
|
||||
PgNumeric::Number {
|
||||
sign: PgNumericSign::Positive,
|
||||
scale: 2,
|
||||
weight: -1,
|
||||
digits: vec![100]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twelve_thousandths() {
|
||||
let twelve_thousandths: BigDecimal = "0.012".parse().unwrap();
|
||||
assert_eq!(
|
||||
PgNumeric::try_from(&twelve_thousandths).unwrap(),
|
||||
PgNumeric::Number {
|
||||
sign: PgNumericSign::Positive,
|
||||
scale: 3,
|
||||
weight: -1,
|
||||
digits: vec![120]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decimal_1() {
|
||||
let decimal: BigDecimal = "1.2345".parse().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user