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

Commit fa035c5

Browse files
committed
Allow construction from Numbers
1 parent 81f3c38 commit fa035c5

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

src/decimal128.mts

+1-10
Original file line numberDiff line numberDiff line change
@@ -790,16 +790,7 @@ export class Decimal128 {
790790
ensureFullySpecifiedConstructorOptions(options);
791791

792792
if ("number" === typeof n) {
793-
if (!Number.isInteger(n)) {
794-
throw new SyntaxError("Non-integer number not permitted");
795-
}
796-
if (!Number.isSafeInteger(n)) {
797-
throw new RangeError(
798-
"Integer is too large to be exactly represented"
799-
);
800-
}
801-
802-
s = n.toString();
793+
s = Object.is(n, -0) ? "-0" : n.toString();
803794
} else if ("bigint" === typeof n) {
804795
s = n.toString();
805796
} else {

tests/constructor.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -587,18 +587,18 @@ describe("number arguments", () => {
587587
expect(new Decimal128(42).toString()).toStrictEqual("42");
588588
});
589589
test("non-integer number", () => {
590-
expect(() => new Decimal128(42.5)).toThrow(SyntaxError);
590+
expect(new Decimal128(42.5).toString()).toStrictEqual("42.5");
591591
});
592592
test("NaN", () => {
593-
expect(() => new Decimal128(NaN)).toThrow(SyntaxError);
593+
expect(new Decimal128(NaN).toString()).toStrictEqual("NaN");
594594
});
595595
test("minus zero", () => {
596-
expect(new Decimal128(-0).toString()).toStrictEqual("0");
596+
expect(new Decimal128(-0).toString()).toStrictEqual("-0");
597597
});
598-
test("too big", () => {
598+
test("very large value gets approximated", () => {
599599
expect(
600-
() => new Decimal128(123456789012345678901234567890123456789)
601-
).toThrow(RangeError);
600+
new Decimal128(123456789012345678901234567890123456789).toString()
601+
).toStrictEqual("123456789012345680000000000000000000000");
602602
});
603603
});
604604

0 commit comments

Comments
 (0)