sourcelibs/Format/demos/invoice.xtl
1⍝!/usr/bin/env xetal
2⍝# An invoice: items, quantities and prices set as a table, money to 2
3⍝# places, a total with thousands separated, the tax as a percentage.
4⍝# Run with "just demo Format invoice".
5
6ᶠ⁼u̲se< "Format"
7ᵗ⁼u̲se< "Strings"
8items ← "notebook" "pen" "stapler" "paper (ream)" "desk"
9qty ← 4 12 1 10 1
10price ← 2.5 0.85 12.99 4.25 1249.0
11amount ← price × f̲loat qty
12
13⍝ The table: a header row, then a row per item (columns joined by c_at_2).
14body ← (5 1 r̲eshape items) c̲at₂ (5 1 r̲eshape 'f̲ormat m̲ap qty) c̲at₂ (5 1 r̲eshape 2 ᶠf̲ixed price) c̲at₂ 5 1 r̲eshape 2 ᶠf̲ixed amount
15ᶠt̲able ("item" "qty" "price" "amount") c̲at body
16
17total ← '+ r̲/ amount
18rate ← 0.0825
19"subtotal " c̲at d̲isclose f̲irst 2 ᶠa̲mount total
20discount ← 0.05 × f̲loat total > 1000.0 ⍝ 5% off orders over 1,000 (a comparison is 0 or 1)
21"discount " c̲at d̲isclose f̲irst 1 ᶠp̲ercent discount
22total ← total × 1.0 − discount
23⍝ Text of several pieces: f_ormat< (X_eTaL's system macro) writes the
24⍝ line as it reads, each value in braces, instead of pieces joined by
25⍝ c_at; a bad format string fails before the program runs.
26@ f̲ormat< "tax {d_isclose f_irst 2 f:p_ercent rate} = {d_isclose f_irst 2 f:f_ixed total * rate}"