Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
Allow construction from Numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
jessealama committed May 7, 2024
1 parent 81f3c38 commit fa035c5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
11 changes: 1 addition & 10 deletions src/decimal128.mts
Original file line number Diff line number Diff line change
Expand Up @@ -790,16 +790,7 @@ export class Decimal128 {
ensureFullySpecifiedConstructorOptions(options);

if ("number" === typeof n) {
if (!Number.isInteger(n)) {
throw new SyntaxError("Non-integer number not permitted");
}
if (!Number.isSafeInteger(n)) {
throw new RangeError(
"Integer is too large to be exactly represented"
);
}

s = n.toString();
s = Object.is(n, -0) ? "-0" : n.toString();
} else if ("bigint" === typeof n) {
s = n.toString();
} else {
Expand Down
12 changes: 6 additions & 6 deletions tests/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,18 @@ describe("number arguments", () => {
expect(new Decimal128(42).toString()).toStrictEqual("42");
});
test("non-integer number", () => {
expect(() => new Decimal128(42.5)).toThrow(SyntaxError);
expect(new Decimal128(42.5).toString()).toStrictEqual("42.5");
});
test("NaN", () => {
expect(() => new Decimal128(NaN)).toThrow(SyntaxError);
expect(new Decimal128(NaN).toString()).toStrictEqual("NaN");
});
test("minus zero", () => {
expect(new Decimal128(-0).toString()).toStrictEqual("0");
expect(new Decimal128(-0).toString()).toStrictEqual("-0");
});
test("too big", () => {
test("very large value gets approximated", () => {
expect(
() => new Decimal128(123456789012345678901234567890123456789)
).toThrow(RangeError);
new Decimal128(123456789012345678901234567890123456789).toString()
).toStrictEqual("123456789012345680000000000000000000000");
});
});

Expand Down

0 comments on commit fa035c5

Please sign in to comment.