Skip to content

Commit

Permalink
Fixes an issue whereby rounding small negative numbers towards zero w…
Browse files Browse the repository at this point in the history
…ould cause negativity to be retained
  • Loading branch information
paupino committed Apr 29, 2021
1 parent bb9b05c commit 010868d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/decimal.rs
Expand Up @@ -964,12 +964,7 @@ impl Decimal {
RoundingStrategy::RoundDown | RoundingStrategy::ToZero => (),
}

Decimal {
lo: value[0],
mid: value[1],
hi: value[2],
flags: flags(negative, dp),
}
Decimal::from_parts(value[0], value[1], value[2], negative, dp)
}

/// Returns a new `Decimal` number with the specified number of decimal points for fractional portion.
Expand Down
17 changes: 17 additions & 0 deletions src/serde.rs
Expand Up @@ -309,4 +309,21 @@ mod test {
assert_eq!(8usize, encoded.len());
}
}

#[test]
#[cfg(all(feature = "serde-str", not(feature = "serde-float")))]
fn bincode_nested_serialization() {
// Issue #361
#[derive(Deserialize, Serialize, Debug)]
pub struct Foo {
value: Decimal,
}

let s = Foo {
value: Decimal::new(-1, 3).round_dp(0),
};
let ser = bincode::serialize(&s).unwrap();
let des: Foo = bincode::deserialize(&ser).unwrap();
assert_eq!(des.value, s.value);
}
}

0 comments on commit 010868d

Please sign in to comment.