1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

LibJS: Support large number of decimals in Number.prototype.toFixed

The spec asks us to perform some calculations that quickly exceed an
`u64`, but instead of jumping through hoops we can rely on our AK
implementation of floating point formatting to come up with the
correctly rounded result.

Note that most other JS engines seem to diverge from the spec as well
and fall back to a generic dtoa path.
This commit is contained in:
Jelle Raaijmakers 2023-10-15 17:02:21 +02:00 committed by Tim Flynn
parent b015926f8e
commit c58193bafa
2 changed files with 35 additions and 34 deletions

View file

@ -31,6 +31,21 @@ describe("correct behavior", () => {
});
});
describe("large number of digits", () => {
test("maximum", () => {
expect((1).toFixed(100)).toBe(
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
);
expect((-3).toFixed(100)).toBe(
"-3.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
);
});
test("fractional values", () => {
expect((1.5).toFixed(30)).toBe("1.500000000000000000000000000000");
});
});
describe("errors", () => {
test("must be called with numeric |this|", () => {
[true, [], {}, Symbol("foo"), "bar", 1n].forEach(value => {