Skip to content

Commit

Permalink
Merge branch 'master' into issue/401
Browse files Browse the repository at this point in the history
  • Loading branch information
paupino committed Jul 21, 2021
2 parents 328ad46 + 2596e62 commit bb347ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ mod test {
assert_eq!("{\"amount\":\"1.234\"}", serialized);
}

#[test]
#[cfg(not(feature = "serde-float"))]
fn serialize_negative_zero() {
let record = Record { amount: -Decimal::ZERO };
let serialized = serde_json::to_string(&record).unwrap();
assert_eq!("{\"amount\":\"-0\"}", serialized);
}

#[test]
#[cfg(feature = "serde-float")]
fn serialize_decimal() {
Expand Down
10 changes: 7 additions & 3 deletions src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ pub(crate) fn to_str_internal(
let len = chars.len();
let whole_len = len - scale;
let mut rep = ArrayString::new();
if append_sign && value.is_sign_negative() {
// Append the negative sign if necessary while also keeping track of the length of an "empty" string representation
let empty_len = if append_sign && value.is_sign_negative() {
rep.push('-');
}
1
} else {
0
};
for i in 0..whole_len + prec {
if i == len - scale {
if i == 0 {
Expand All @@ -58,7 +62,7 @@ pub(crate) fn to_str_internal(
}

// corner case for when we truncated everything in a low fractional
if rep.is_empty() {
if rep.len() == empty_len {
rep.push('0');
}

Expand Down

0 comments on commit bb347ca

Please sign in to comment.