Skip to content

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.

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 rate = £280
labour = days on site * day rate
subtotal = tiles + grout and adhesive + labour
vat = 20% of subtotal
subtotal + vat

The 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.

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%)::principal

The 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.

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%)::principal

Payments 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.

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 1 dp
target = $10,000
solve for monthly: monthly * 36 + $1,200 = target

A bare solve for handles the one-line case: how much per month, over three years, on top of what is already saved.

Percent 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.60

Stacked discounts compound, so 20% then 10% is 28% off, not 30%. The last line proves it by solving for the single equivalent discount.

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 dp

unit 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.