From 4ee0c83548e9cf2fd86a5636fd52cacffc950512 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Mon, 28 Nov 2022 14:27:50 +0100 Subject: [PATCH] [JS] Fix a rounding issue in printf (bug 1802888) --- src/scripting_api/util.js | 7 ++++--- test/unit/scripting_spec.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/scripting_api/util.js b/src/scripting_api/util.js index 6c8130ceb..262c1f0aa 100644 --- a/src/scripting_api/util.js +++ b/src/scripting_api/util.js @@ -155,10 +155,11 @@ class Util extends PDFObject { } if (decPart.length > 2) { decPart = `${decimalSep}${decPart.substring(2)}`; - } else if (cFlags & HASH) { - decPart = "."; } else { - decPart = ""; + if (decPart === "1") { + intPart += Math.sign(arg); + } + decPart = cFlags & HASH ? "." : ""; } } diff --git a/test/unit/scripting_spec.js b/test/unit/scripting_spec.js index 9ae19cf3d..bfcc10a56 100644 --- a/test/unit/scripting_spec.js +++ b/test/unit/scripting_spec.js @@ -280,6 +280,18 @@ describe("Scripting", function () { `util.printf("Decimal number: %,0.2f", -12.34567)` ); expect(value).toEqual("Decimal number: -12.35"); + + value = await myeval(`util.printf("Decimal number: %,0.0f", 4.95)`); + expect(value).toEqual("Decimal number: 5"); + + value = await myeval(`util.printf("Decimal number: %,0.0f", 4.49)`); + expect(value).toEqual("Decimal number: 4"); + + value = await myeval(`util.printf("Decimal number: %,0.0f", -4.95)`); + expect(value).toEqual("Decimal number: -5"); + + value = await myeval(`util.printf("Decimal number: %,0.0f", -4.49)`); + expect(value).toEqual("Decimal number: -4"); }); it("should print a string with no argument", async () => {