Skip to content

Commit

Permalink
Fix power calculation when encoding the BigDecimal into NUMERIC (#1692)
Browse files Browse the repository at this point in the history
* Show failing test

0.002 will be encoded as 0.02

* The power calculation should depend on the offset

of the digits
  • Loading branch information
VersBinarii authored Feb 11, 2022
1 parent f3ac717 commit fd2d26e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sqlx-core/src/postgres/types/bigdecimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric {
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));
digits.push(base_10_to_10000(&base_10) * 10i16.pow((offset - base_10.len()) as u32));
}

if let Some(rest) = base_10.get(offset..) {
Expand Down
6 changes: 6 additions & 0 deletions tests/postgres/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ test_type!(bigdecimal<sqlx::types::BigDecimal>(Postgres,
"0.01234567::numeric" == "0.01234567".parse::<sqlx::types::BigDecimal>().unwrap(),
"0.012345678::numeric" == "0.012345678".parse::<sqlx::types::BigDecimal>().unwrap(),
"0.0123456789::numeric" == "0.0123456789".parse::<sqlx::types::BigDecimal>().unwrap(),
"0.002::numeric" == "0.002".parse::<sqlx::types::BigDecimal>().unwrap(),
"0.0002::numeric" == "0.0002".parse::<sqlx::types::BigDecimal>().unwrap(),
"0.00002::numeric" == "0.00002".parse::<sqlx::types::BigDecimal>().unwrap(),
"0.000002::numeric" == "0.000002".parse::<sqlx::types::BigDecimal>().unwrap(),
"0.0000002::numeric" == "0.0000002".parse::<sqlx::types::BigDecimal>().unwrap(),
"0.00000002::numeric" == "0.00000002".parse::<sqlx::types::BigDecimal>().unwrap(),
"12.34::numeric" == "12.34".parse::<sqlx::types::BigDecimal>().unwrap(),
"12345.6789::numeric" == "12345.6789".parse::<sqlx::types::BigDecimal>().unwrap(),
));
Expand Down

0 comments on commit fd2d26e

Please sign in to comment.