Help and docs
Sign in

demo/acme-web code browser

main 72fe66b
TypeScript · 16 lines · 606 B
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import type { Money } from "../api/client";

const zeroDecimal = new Set(["JPY", "KRW"]);

// formatMoney renders an amount for people: "$12.50", "¥1,200".
export function formatMoney(m: Money, locale = navigator.language): string {
  const digits = zeroDecimal.has(m.currency) ? 0 : 2;
  return new Intl.NumberFormat(locale, { style: "currency", currency: m.currency }).format(
    m.minor / 10 ** digits,
  );
}

// addMoney sums amounts in one currency.
export function addMoney(currency: string, amounts: Money[]): Money {
  return { currency, minor: amounts.reduce((sum, a) => sum + a.minor, 0) };
}