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

Commit 9d19da8

Browse files
committed
Adjust examples
1 parent 2079505 commit 9d19da8

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

examples/bill.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ const zero = new Decimal128("0");
44
const one = new Decimal128("1");
55

66
function calculateBill(items, tax) {
7-
let taxRate = Decimal128.add(new Decimal128(tax), one);
7+
let taxRate = new Decimal128(tax).add(one);
88
let total = items.reduce((total, { price, count }) => {
9-
return Decimal128.multiply(
10-
Decimal128.add(total, new Decimal128(price)),
11-
new Decimal128(count)
12-
);
9+
return total.add(new Decimal128(price).multiply(new Decimal128(count)));
1310
}, zero);
14-
return Decimal128.multiply(total, taxRate);
11+
return total.multiply(taxRate);
1512
}
1613

1714
const items = [

examples/floor.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function floor(d) {
2+
return d.round(0, "floor");
3+
}

examples/mortgage.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ function calculateMonthlyPayment(p, r, y) {
88
let principal = new Decimal128(p);
99
let annualInterestRate = new Decimal128(r);
1010
let years = new Decimal128(y);
11-
const monthlyInterestRate = Decimal128.divide(
12-
annualInterestRate,
13-
paymentsPerYear
14-
);
15-
const paymentCount = Decimal128.multiply(paymentsPerYear, years);
16-
const onePlusInterestRate = Decimal128.add(one, monthlyInterestRate);
11+
const monthlyInterestRate = annualInterestRate.divide(paymentsPerYear);
12+
const paymentCount = paymentsPerYear.multiply(years);
13+
const onePlusInterestRate = monthlyInterestRate.add(one);
1714
const ratePower = pow(onePlusInterestRate, paymentCount);
1815

19-
let x = Decimal128.multiply(principal, monthlyInterestRate);
20-
let numerator = Decimal128.multiply(x, ratePower);
16+
let x = principal.multiply(monthlyInterestRate);
17+
let numerator = x.multiply(ratePower);
2118

22-
let denominator = Decimal128.subtract(ratePower, one);
19+
let denominator = ratePower.subtract(one);
2320

24-
return Decimal128.divide(numerator, denominator);
21+
return numerator.divide(denominator);
2522
}
2623

2724
console.log(calculateMonthlyPayment("5000000", "0.05", "30").toString());

examples/step.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ function stepUp(d, n, x) {
1111

1212
let starting = new Decimal128("1.23");
1313
let stepped = stepUp(starting, new Decimal128("3"), new Decimal("-4"));
14-
console.log(stepped.toFixed(4)); // 1.2305
14+
console.log(stepped.toString({ numDecimalDigits: 4 })); // 1.2305

0 commit comments

Comments
 (0)