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

Commit 32e49b4

Browse files
committed
Normalize by default
Also, make the API match what the spec says: remove `toExponentialString` and `toDecimalPlaces`, bringing their functionality into `toString`, which now takes options.
1 parent 98302ec commit 32e49b4

14 files changed

+265
-332
lines changed

src/decimal128.mts

+150-114
Large diffs are not rendered by default.

tests/abs.test.js

-30
This file was deleted.

tests/add.test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ describe("addition", () => {
5858
describe("non-normalized", () => {
5959
test("one point zero plus one point zero", () => {
6060
expect(
61-
new Decimal128("1.0").add(new Decimal128("1.0")).toString()
61+
new Decimal128("1.0", { normalize: false })
62+
.add(new Decimal128("1.0", { normalize: false }), {
63+
normalize: false,
64+
})
65+
.toString()
6266
).toStrictEqual("2.0");
6367
});
6468
});
@@ -123,7 +127,7 @@ describe("examples from the General Decimal Arithmetic specification", () => {
123127
expect(
124128
new Decimal128("1E2")
125129
.add(new Decimal128("1E4"))
126-
.toExponentialString()
130+
.toString({ format: "exponential" })
127131
).toStrictEqual("1.01E+4");
128132
});
129133
});

tests/constructor.test.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ describe("constructor", () => {
77
test("sane string works", () => {
88
expect(new Decimal128("123.456")).toBeInstanceOf(Decimal128);
99
});
10-
test("no normalization", () => {
11-
expect(new Decimal128("1.20").toString()).toStrictEqual("1.20");
10+
test("normalization by default", () => {
11+
expect(new Decimal128("1.20").toString()).toStrictEqual("1.2");
12+
});
13+
test("normalization can be disabled", () => {
14+
expect(
15+
new Decimal128("1.20", { normalize: false }).toString()
16+
).toStrictEqual("1.20");
1217
});
1318
test("no normalization (exponential notation) (positive exponent)", () => {
1419
let d = new Decimal128("1.20E1");
@@ -436,18 +441,17 @@ describe("rounding options", () => {
436441
new Decimal128("0.1", { foo: "bar" }).toString()
437442
).toStrictEqual("0.1");
438443
});
439-
test("unknown rounding mode throws", () => {
444+
test("unknown rounding mode works out to default", () => {
440445
expect(
441-
() => new Decimal128("0.1", { roundingMode: "jazzy" })
442-
).toThrow(Error);
446+
new Decimal128("0.5", { roundingMode: "jazzy" }).toString()
447+
).toStrictEqual("0.5");
443448
});
444449
test("unknown rounding mode throws on large input", () => {
445450
expect(
446-
() =>
447-
new Decimal128("0." + "9".repeat(10000), {
448-
roundingMode: "cool",
449-
})
450-
).toThrow(Error);
451+
new Decimal128("0." + "9".repeat(10000), {
452+
roundingMode: "cool",
453+
}).toString()
454+
).toStrictEqual("1.000000000000000000000000000000000");
451455
});
452456
});
453457
describe("negative value, final decimal digit is five, penultimate digit is less than nine", () => {

tests/divide.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ describe("examples from the General Decimal Arithmetic Specification", () => {
162162
});
163163
test("example six", () => {
164164
expect(
165-
new Decimal128("8.00").divide(new Decimal128("2")).toString()
165+
new Decimal128("8.00", { normalize: false })
166+
.divide(new Decimal128("2"), { normalize: false })
167+
.toString()
166168
).toStrictEqual("4.00");
167169
});
168170
test("example seven", () => {
@@ -184,7 +186,7 @@ describe("examples from the General Decimal Arithmetic Specification", () => {
184186
expect(
185187
new Decimal128("2.40E+6")
186188
.divide(new Decimal128("2"))
187-
.toExponentialString()
189+
.toString({ format: "exponential" })
188190
).toStrictEqual("1.20E+6");
189191
});
190192
});

tests/integer.test.js

-34
This file was deleted.

tests/multiply.test.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const examples = [
2222

2323
function checkProduct(a, b, c) {
2424
expect(
25-
new Decimal128(a).multiply(new Decimal128(b)).toString()
25+
new Decimal128(a)
26+
.multiply(new Decimal128(b), { normalize: false })
27+
.toString()
2628
).toStrictEqual(c);
2729
}
2830

@@ -184,8 +186,8 @@ describe("examples from the General Decimal Arithmetic specification", () => {
184186
// slightly modified because we have more precision
185187
expect(
186188
new Decimal128("654321")
187-
.multiply(new Decimal128("654321"))
188-
.toExponentialString()
189+
.multiply(new Decimal128("654321"), { normalize: false })
190+
.toString({ format: "exponential" })
189191
).toStrictEqual("4.28135971041E+11");
190192
});
191193
});

tests/negate.test.js

-30
This file was deleted.

tests/remainder.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ describe("examples from the General Decimal Arithmetic Specification", () => {
121121
});
122122
test("example six", () => {
123123
expect(
124-
new Decimal128("3.6").remainder(new Decimal128("1.3")).toString()
124+
new Decimal128("3.6")
125+
.remainder(new Decimal128("1.3"), { normalize: false })
126+
.toString()
125127
).toStrictEqual("1.0");
126128
});
127129
});

tests/round.test.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,14 @@ describe("rounding", () => {
5050
"1.5"
5151
);
5252
});
53-
test("more digits than are available", () => {
54-
expect(new Decimal128("1.5").round(2).toString()).toStrictEqual(
55-
"1.50"
56-
);
57-
});
5853
test("negative odd", () => {
5954
expect(new Decimal128("-1.5").round(1).toString()).toStrictEqual(
6055
"-1.5"
6156
);
6257
});
6358
test("round down (positive)", () => {
6459
expect(new Decimal128("1.1").round(6).toString()).toStrictEqual(
65-
"1.100000"
60+
"1.1"
6661
);
6762
});
6863
});

tests/subtract.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("subtraction", () => {
88
test("subtract decimal part", () => {
99
expectDecimal128(
1010
new Decimal128("123.456").subtract(new Decimal128("0.456")),
11-
"123.000"
11+
"123"
1212
);
1313
});
1414
test("minus negative number", () => {
@@ -141,7 +141,9 @@ describe("examples from the General Decimal Arithmetic specification", () => {
141141
});
142142
test("example two", () => {
143143
expect(
144-
new Decimal128("1.3").subtract(new Decimal128("1.30")).toString()
144+
new Decimal128("1.3")
145+
.subtract(new Decimal128("1.30", { normalize: false }))
146+
.toString()
145147
).toStrictEqual("0.00");
146148
});
147149
test("example three", () => {

tests/todecimalplaces.test.js

-41
This file was deleted.

tests/toexponentialstring.test.js

-57
This file was deleted.

0 commit comments

Comments
 (0)