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

Strengthen README to make it clear that this is a prototype for proposal-decimal #77

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use TypeScript for examples
jessealama committed Feb 1, 2024
commit 0d86d4ddb31e0641446f4978545ef67370486c16
12 changes: 8 additions & 4 deletions examples/bill.js → examples/bill.ts
Original file line number Diff line number Diff line change
@@ -3,17 +3,21 @@ import { Decimal128 } from "../src/decimal128.mjs";
const zero = new Decimal128("0");
const one = new Decimal128("1");

function calculateBill(items, tax) {
let taxRate = new Decimal128(tax).add(one);
interface Item {
price: string;
count: string;
}

function calculateBill(items: Item[], tax: string): Decimal128 {
let total = items.reduce((total, { price, count }) => {
return total.add(new Decimal128(price).multiply(new Decimal128(count)));
}, zero);
return total.multiply(taxRate);
return total.multiply(new Decimal128(tax).add(one));
}

const items = [
{ price: "1.25", count: "5" },
{ price: "5.00", count: "1" },
];
const tax = "0.0735";
console.log(calculateBill(items, tax).toString());
console.log(calculateBill(items, tax).toString({ numDecimalDigits: 2}));
3 changes: 0 additions & 3 deletions examples/floor.js

This file was deleted.

7 changes: 7 additions & 0 deletions examples/floor.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Decimal128 } from "../src/decimal128.mjs";

function floor(d: Decimal128): Decimal128 {
return d.round(0, "floor");
}

export { floor };
6 changes: 0 additions & 6 deletions examples/normalize.js

This file was deleted.

8 changes: 8 additions & 0 deletions examples/normalize.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Decimal128 } from "../src/decimal128.mjs";

function normalize(d: Decimal128): string {
// Decimal128 object
return d.toString({ normalize: true });
}

export { normalize };
8 changes: 2 additions & 6 deletions examples/pow.js → examples/pow.mts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Decimal128 } from "../src/decimal128.mjs";

const one = new Decimal128("1");

function pow(a, b) {
let i = one;
function pow(a: Decimal128, b: number): Decimal128 {
let result = a;
while (-1 === i.cmp(b)) {
for (let i = 0; i < b; i++) {
result = result.multiply(a);
i = i.add(one);
}
return result;
}
14 changes: 0 additions & 14 deletions examples/step.js

This file was deleted.