Money
Bills, quotes, loans, savings, and prices, with the currency carried through.
Split the bill, with tip, in another currency
Section titled “Split the bill, with tip, in another currency”Money keeps its currency through percentages and division. The conversion at the end uses the day’s rate.
dinner = $86.40
with tip = dinner + 18%
per person = with tip / 4
per person to GBP+ 18% scales the amount, / 4 leaves the currency alone, and to GBP
converts only the final line. The rate is fetched when the page loads.
A quote with materials, labour, and VAT
Section titled “A quote with materials, labour, and VAT”Phrases as names make a quote read like the note you would have written
anyway. # lines are comments; the other lines are the arithmetic.
## Bathroom refit
# materials
tiles = 12 m^2 * £38 / 1 m^2
grout and adhesive = £64
# labour
days on site = 3 day
day rate = £280 per day
labour = days on site * day rate
subtotal = total
vat = 20% of subtotal
subtotal + vatThe tile price is a rate, pounds per square metre, and the area cancels it to
pounds. If you typed 12 m^2 * £38 by mistake, the answer would carry m^2
and you would notice. total adds up the lines above it that are the same
kind as the one just above: the three costs in pounds, not the days or the
day rate.
A loan you can run backwards
Section titled “A loan you can run backwards”A formula is a set of equations. Give it the principal and it returns the payment; give it the payment you can afford and it returns the principal.
formula Loan(principal: Currency, rate, years = 30):
let monthly rate = rate / 12
let n = years * 12
payment = principal * monthly rate / (1 - (1 + monthly rate) ^ (0 - n))
house = $385,000
deposit = 20% of house
Loan(principal = house - deposit, rate = 6.5%)::payment
Loan(principal = house - deposit, rate = 6.5%, years = 15)::payment
Loan(payment = $1,500, rate = 6.5%)::principalThe two let lines are working values that stay inside the formula. years
has a default, so the first call omits it. The last call gives payment and
asks for principal, and smoot rearranges the equation, including the power.
Overpay a mortgage and see what it saves
Section titled “Overpay a mortgage and see what it saves”The finance/mortgage module bundles the arithmetic. Overpayment
composes two of its formulas: the standard repayment, then how long the
loan lasts at that repayment plus the extra.
import "finance/mortgage"
plan = Overpayment(principal = £280,000, apr = 4.5%, term = 25 years, extra = £150/month)
plan::new payment
plan::term saved to years, months to 0 dp
plan::interest saved
Mortgage(monthly payment = £1,400/month, apr = 4.5%)::principalPayments are rates, pounds per month, so the module can multiply one by a
term and get money back. The last line runs Mortgage backwards: the
largest loan a monthly budget will carry.
Try a higher rate, and a bigger deposit
Section titled “Try a higher rate, and a bigger deposit”A what if line tries other inputs without touching the document. Each one
gets a column beside the answers, so you can compare them.
import "finance/mortgage"
house = £320,000
deposit = £64,000
rate = 5%
loan = house - deposit
payment = Mortgage(principal = loan, apr = rate)::monthly payment to 2 dp
what if rates up: rate = 6%
what if both:
rate = 6%
deposit = £96,000
rates up::rateOnly the lines that depend on a change get a cell. house has none. In
rates up the payment goes from £1,496.55 to £1,649.41 a month. In both
the bigger deposit more than pays for the higher rate: £1,443.24. The last
line reads one input back from a what-if: rates up::rate is 6%.
Savings goals with compound interest
Section titled “Savings goals with compound interest”The prelude’s CompoundInterest formula answers in any direction. For the
number of periods, ask for it.
CompoundInterest(principal = $5,000, rate = 4%, periods = 10)::amount
CompoundInterest(amount = $10,000, rate = 4%, principal = $5,000)::periods to nearest 1 rounded up
target = $10,000
solve for monthly: monthly * 36 + $1,200 = targetA bare solve for handles the one-line case: how much per month, over three
years, on top of what is already saved.
Original price from a sale price
Section titled “Original price from a sale price”Percentage arithmetic is invertible, so the unknown can be on the left of the percentage.
solve for original: original - 25% = $90
sneakers = $80
sneakers - 20% - 10%
solve for combined: $80 - combined = $57.60Stacked discounts compound, so 20% then 10% is 28% off, not 30%. The last line proves it by solving for the single equivalent discount.
Price an API by the token
Section titled “Price an API by the token”Anything countable can be a dimension. Declare it, give it a unit and a prefix-sized multiple, and the money follows.
unit token of Tokens, also: tokens
unit ktoken = 1000 token, also: ktokens
input price = $0.30 / 1 ktoken
output price = $1.20 / 1 ktoken
per request = 1200 tokens * input price + 300 tokens * output price
requests per day = 40,000
per request * requests per day * 30 to 2 dpunit token of Tokens creates the dimension and its base unit in one line.
Dividing a price by 1 ktoken makes a rate, and multiplying by tokens cancels
it back to dollars.