Skip to content

Commit

Permalink
[JS] Fix a rounding issue in printf (bug 1802888)
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman authored and pull[bot] committed Dec 8, 2022
1 parent 8d07aad commit 42eb673
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/scripting_api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? "." : "";
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 42eb673

Please sign in to comment.