Skip to content

Commit

Permalink
Merge 062cad1 into 645b092
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekenre committed Oct 11, 2018
2 parents 645b092 + 062cad1 commit b3c1d33
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cbor2/encoder.py
Expand Up @@ -171,7 +171,8 @@ def encode_decimal(encoder, value):
encoder.write(b'\xf9\x7c\x00' if value > 0 else b'\xf9\xfc\x00')
else:
dt = value.as_tuple()
mantissa = sum(d * 10 ** i for i, d in enumerate(reversed(dt.digits)))
negation = (1, -1)[dt.sign] # sign is 0 for positive numbers and 1 for negative
mantissa = negation * sum(d * 10 ** i for i, d in enumerate(reversed(dt.digits)))
with encoder.disable_value_sharing():
encode_semantic(encoder, CBORTag(4, [dt.exponent, mantissa]))

Expand Down
3 changes: 2 additions & 1 deletion tests/test_encoder.py
Expand Up @@ -134,10 +134,11 @@ def test_naive_datetime():

@pytest.mark.parametrize('value, expected', [
(Decimal('14.123'), 'c4822219372b'),
(Decimal('-14.123'), 'C4822239372A'),
(Decimal('NaN'), 'f97e00'),
(Decimal('Infinity'), 'f97c00'),
(Decimal('-Infinity'), 'f9fc00')
], ids=['normal', 'nan', 'inf', 'neginf'])
], ids=['normal', 'negative', 'nan', 'inf', 'neginf'])
def test_decimal(value, expected):
expected = unhexlify(expected)
assert dumps(value) == expected
Expand Down

0 comments on commit b3c1d33

Please sign in to comment.