Skip to content

Commit

Permalink
Merge pull request #18737 from calixteman/bug1918115
Browse files Browse the repository at this point in the history
[JS] Correctly format floating numbers when they're close to an integer (bug 1918115)
  • Loading branch information
calixteman authored Sep 11, 2024
2 parents c52e848 + ca95264 commit cb20d1b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/scripting_api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ class Util extends PDFObject {
? Math.abs(arg - intPart).toFixed(nPrecision)
: Math.abs(arg - intPart).toString();
if (decPart.length > 2) {
decPart = `${decimalSep}${decPart.substring(2)}`;
if (/^1\.0+$/.test(decPart)) {
intPart += Math.sign(arg);
decPart = `${decimalSep}${decPart.split(".")[1]}`;
} else {
decPart = `${decimalSep}${decPart.substring(2)}`;
}
} else {
if (decPart === "1") {
intPart += Math.sign(arg);
Expand Down
28 changes: 28 additions & 0 deletions test/integration/scripting_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2468,4 +2468,32 @@ describe("Interaction", () => {
);
});
});

describe("Correctly format numbers", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("bug1918115.pdf", getSelector("33R"));
});

afterAll(async () => {
await closePages(pages);
});

it("must check that the computed value is correct", async () => {
await Promise.all(
pages.map(async ([browserName, page], i) => {
await waitForScripting(page);

const inputSelector = getSelector("33R");
await page.click(inputSelector);
await page.type(inputSelector, "7");
await page.click(getSelector("34R"));
await page.waitForFunction(
`${getQuerySelector("35R")}.value === "324,00"`
);
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,4 @@
!bug1708040.pdf
!issue18694.pdf
!issue18693.pdf
!bug1918115.pdf
Binary file added test/pdfs/bug1918115.pdf
Binary file not shown.

0 comments on commit cb20d1b

Please sign in to comment.