Back to the gallery

Code Workspace

shop6
import type { CartItem } from "./cart"
​
const TAX_RATE = 0.2
​
// Prices stay in cents until they're shown, so they never drift.
export function totalPrice(items: CartItem[]) {
const subtotal = items.reduce(
(sum, item) => sum + item.price * item.quantity,
0
)
return Math.round(subtotal * (1 + TAX_RATE))
}
​
export function formatPrice(cents: number) {
return new Intl.NumberFormat("en-GB", {
style: "currency",
currency: "GBP",
}).format(cents / 100)
}