Skip to content

[JS] Correctly format floating numbers when they're close to an integer (bug 1918115) #18737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.