decimal128
Version:
Partial implementation of IEEE 754 Decimal128 decimal floating-point numbers
24 lines (19 loc) • 633 B
text/typescript
import { Decimal128 } from "../src/Decimal128.mjs";
const zero = new Decimal128("0");
const one = new Decimal128("1");
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(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).toFixed({ digits: 2 }));