Currency: Difference between revisions

146 bytes removed ,  3 years ago
→‎{{header|J}}: Create bill using verb
(→‎{{header|J}}: Create bill using verb)
Line 675:
 
<lang j>require 'format/printf'
fmtD=: 0j2&": NB. format rational as decimal
Items=: ;: 'Hamburger Milkshake'
 
Quantities=: 4000000000000000 2
Items=: ;: 'Hamburger Milkshake'
QuantitiesPrices=: x: 4000000000000000 5.50 2.86
Prices=: x: 5.50 2.86
Tax_rate=: x: 0.0765
Values=: Quantities * Prices
Subtotal=: +/ Values
Tax=: Tax_rate * Subtotal
Total=: Subtotal + Tax
 
OutputTemplatemakeBill=: nounverb define
'items prices quantities'=. y
Item Price Quantity Value
values=. prices * quantities
%9s %8s %20d %22s
subtotal=. +/ values
%9s %8s %20d %22s
Tax tax=:. Tax_rate * Subtotalsubtotal
-------------------------------
total=. subtotal + tax
Subtotal: %20s
 
Tax: %20s
'%9s %8s %20s %22s' printf ;:'Item Price Quantity Value'
Total: %20s
'%9s %8.2f %20d %22.2f' printf"1 items ,. <"0 prices ,. quantities ,. values
'%62s' printf <'-------------------------------'
'%40s %21.2f' printf"1 (;:'Subtotal: Tax: Total:') ,. subtotal;tax;total
)
 
makeBill Items;Prices;Quantities</lang>
Vals=: (,Items ,. (fmtD&.> Prices) ,. (<"0 Quantities) ,. (fmtD&.> Values)) , fmtD&.> Subtotal,Tax,Total
OutputTemplate printf Vals</lang>
{{out}}
<pre>Item Item Price Quantity Value
Hamburger 5.50 4000000000000000 22000000000000000.00
Milkshake 2.86 2 5.72
892

edits