diff --git a/changelog.md b/changelog.md index 6e9369e..8fd82d3 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,9 @@ ChangeLog 2.1.1 (????-??-??) ------------------ +* Decimals that round to a whole number, such as `1.0001`, now serialize as + `1.0` instead of `1.`, which is invalid and was rejected by the parser. + (@gyanu2507) * Fix: serialization of displaystring that encodes to byte values under 0xf, they were missing the 0 padding of %0?. diff --git a/src/serializer.ts b/src/serializer.ts index 88d16f3..35f634f 100644 --- a/src/serializer.ts +++ b/src/serializer.ts @@ -129,7 +129,8 @@ export function serializeDecimal(input: number): string { if (signifantDigits > 12) { throw new SerializeError('Fractional numbers are not allowed to have more than 12 significant digits before the decimal point'); } - return out; + // A decimal must have at least one fractional digit. + return out.endsWith('.') ? out + '0' : out; } export function serializeString(input: string): string { diff --git a/test/serializer-tests.js b/test/serializer-tests.js index 6991d9a..54894e9 100644 --- a/test/serializer-tests.js +++ b/test/serializer-tests.js @@ -101,6 +101,16 @@ describe('serializer shorthands', () => { describe('serializeItem', () => { + it('should keep a fractional digit when a decimal rounds to a whole number', () => { + + for (const [input, expected] of [[1.0001, '1.0'], [9.9999, '10.0'], [-0.0001, '-0.0']]) { + const str = serializeItem(input); + assert.equal(str, expected); + assert.deepEqual(parseItem(str), [Number(expected), new Map()]); + } + + }); + it('should error when passing a type that\'s not recognized', () => { let caught = false;