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

Commit 98302ec

Browse files
committed
Add more examples
1 parent db49f27 commit 98302ec

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

examples/pow.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ function pow(a, b) {
66
let i = one;
77
let result = a;
88
while (-1 === i.cmp(b)) {
9-
result = Decimal128.multiply(result, a);
10-
i = Decimal128.add(i, one);
9+
result = result.multiply(a);
10+
i = i.add(one);
1111
}
1212
return result;
1313
}

examples/round.js

+71
Original file line numberDiff line numberDiff line change
@@ -1 +1,72 @@
11
// Example with different rounding modes
2+
3+
// Examples from the Intl.NumberFormat spec
4+
5+
let minusOnePointFive = new Decimal128("-1.5");
6+
let zeroPointFour = new Decimal128("0.4");
7+
let zeroPointFive = new Decimal128("0.5");
8+
let zeroPointSix = new Decimal128("0.6");
9+
let onePointFive = new Decimal128("1.5");
10+
11+
// ceiling
12+
"-1" === minusOnePointFive.round(0, "ceil").toString();
13+
"1" === zeroPointFour.round(0, "ceil").toString();
14+
"1" === zeroPointFive.round(0, "ceil").toString();
15+
"1" === zeroPointSix.round(0, "ceil").toString();
16+
"2" === onePointFive.round(0, "ceil").toString();
17+
18+
// floor
19+
"-2" === minusOnePointFive.round(0, "floor").toString();
20+
"0" === zeroPointFour.round(0, "floor").toString();
21+
"0" === zeroPointFive.round(0, "floor").toString();
22+
"0" === zeroPointSix.round(0, "floor").toString();
23+
"1" === onePointFive.round(0, "floor").toString();
24+
25+
// expand
26+
"-2" === minusOnePointFive.round(0, "expand").toString();
27+
"1" === zeroPointFour.round(0, "expand").toString();
28+
"1" === zeroPointFive.round(0, "expand").toString();
29+
"1" === zeroPointSix.round(0, "expand").toString();
30+
"2" === onePointFive.round(0, "expand").toString();
31+
32+
// truncate
33+
"-1" === minusOnePointFive.round(0, "trunc").toString();
34+
"0" === zeroPointFour.round(0, "trunc").toString();
35+
"0" === zeroPointFive.round(0, "trunc").toString();
36+
"0" === zeroPointSix.round(0, "trunc").toString();
37+
"1" === onePointFive.round(0, "trunc").toString();
38+
39+
// round ties to ceiling
40+
"-1" === minusOnePointFive.round(0, "halfCeil").toString();
41+
"0" === zeroPointFour.round(0, "halfCeil").toString();
42+
"1" === zeroPointFive.round(0, "halfCeil").toString();
43+
"1" === zeroPointSix.round(0, "halfCeil").toString();
44+
"2" === onePointFive.round(0, "halfCeil").toString();
45+
46+
// round ties to floor
47+
"-2" === minusOnePointFive.round(0, "halfFloor").toString();
48+
"0" === zeroPointFour.round(0, "halfFloor").toString();
49+
"0" === zeroPointFive.round(0, "halfFloor").toString();
50+
"1" === zeroPointSix.round(0, "halfFloor").toString();
51+
"1" === onePointFive.round(0, "halfFloor").toString();
52+
53+
// round ties away from zero
54+
"-2" === minusOnePointFive.round(0, "halfExpand").toString();
55+
"0" === zeroPointFour.round(0, "halfExpand").toString();
56+
"1" === zeroPointFive.round(0, "halfExpand").toString();
57+
"1" === zeroPointSix.round(0, "halfExpand").toString();
58+
"2" === onePointFive.round(0, "halfExpand").toString();
59+
60+
// round ties to toward zero
61+
"-1" === minusOnePointFive.round(0, "halfTrunc").toString();
62+
"0" === zeroPointFour.round(0, "halfTrunc").toString();
63+
"0" === zeroPointFive.round(0, "halfTrunc").toString();
64+
"1" === zeroPointSix.round(0, "halfTrunc").toString();
65+
"1" === onePointFive.round(0, "halfTrunc").toString();
66+
67+
// round ties to even
68+
"-2" === minusOnePointFive.round(0, "halfEven").toString();
69+
"0" === zeroPointFour.round(0, "halfEven").toString();
70+
"0" === zeroPointFive.round(0, "halfEven").toString();
71+
"1" === zeroPointSix.round(0, "halfEven").toString();
72+
"2" === onePointFive.round(0, "halfEven").toString();

examples/step.js

+13
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
// Example for stepping a value up/down
2+
import { pow } from "./pow.js";
3+
import { Decimal128 } from "../src/decimal128.mjs";
4+
5+
const ten = new Decimal128("10");
6+
7+
function stepUp(d, n, x) {
8+
let increment = pow(ten, x);
9+
return d.add(n.times(increment));
10+
}
11+
12+
let starting = new Decimal128("1.23");
13+
let stepped = stepUp(starting, new Decimal128("3"), new Decimal("-4"));
14+
console.log(stepped.toFixed(4)); // 1.2305

0 commit comments

Comments
 (0)