Writing a calculation line
This page covers the basics that every calc block relies on: how a line is read, how to leave notes beside your sums, how to name values and reuse them, and how to make a line depend on a condition. Start here if you are new to calculations in Varlig, and come back to it when a name or a comment doesn’t behave the way you expect.
One expression per line
Calculations live in a calc block inside a note. Type a ```calc fence, or press ⌘Y to
insert one. Each line in the block is one calculation, and its answer appears beside the line.
£3.40 * 5 gives £17.0045 min + 1 hour 20 min gives 2 hours 5 min1.5 litres in ml gives 1,500 mLBlank lines and comment lines have no answer, so you can space a block out and explain it as you go.
Varlig reads the whole line as maths first, including everyday phrases such as
£45 with 20% VAT. If the line doesn’t work that way, it sets aside the words it doesn’t
recognise, so 5 apples + 3 oranges still comes to 8. That’s handy for quick jottings, but a
description can collide with a phrase or a unit name, so when you want to say what a line is
for, use a comment or a label, described next.
How Varlig reads a phrase has the details.
Comments
A comment is text the calculator ignores. There are three ways to write one:
| Form | Example | What is ignored |
|---|---|---|
| Hash comment | rent * 12 # per year or # Budget |
Everything from # to the end of the line |
| Slash comment | rent * 12 // per year |
Everything from // to the end of the line |
| Quoted note | "flat whites" £3.40 * 5 |
Anything inside double quotes, anywhere on a line |
# Weekly shopmilk = £1.45bread = £1.20 // sourdougheggs = £2.30 "free range"milk + bread + eggs # without the extras gives £4.95A line that starts with # is a comment from start to finish, which makes it a handy heading
inside a block.
Labels
A label is a few words followed by a colon and a space at the start of a line. It describes the answer without being part of the maths:
rent = £1,250bills = £180Rent and bills: rent + bills gives £1,430.00Café bill: £12.50 + £4 gives £16.50Labels can use letters (including accented ones), spaces and underscores, but not digits.
A label does not create a name. After Rent and bills: rent + bills, there is no value
called Rent and bills. Use = when you want to reuse a result. Also avoid putting a label in
front of an assignment: Housing: rent = £1,250 shows the amount, but rent is not stored.
Assignment
= gives a value a name. The line also shows the value, so you can check it as you go.
price = £85 gives £85.00price + 20% gives £102.00To update a name you have already defined, use +=, -=, *= or /=:
holiday fund = £400holiday fund += £150 gives £550.00holiday fund -= £85 gives £465.00servings = 4servings *= 3 gives 12flour = 750 gflour /= 3 gives 250 gIf the right-hand side fails, the name holds the error, and every line that uses it shows the same error until you assign the name again:
guests = 0per head = £120 / guests gives Error: divide by zeroper head + £5 gives Error: divide by zeroNames
Names can be several words long, and can contain accented and non-Latin letters, digits, underscores and emoji:
flat white = £3.40flat white * 5 gives £17.00crème brûlée = £6.50crème brûlée * 2 gives £13.00🍎 price = £0.45🍎 price * 6 gives £2.70q1 sales = £12,400q2 sales = £14,100q1 sales + q2 sales gives £26,500.00bedroom_width = 3.2 mbedroom_width * 2 gives 6.4 mA name can include words that Varlig uses elsewhere, such as to, in or needed, and it can
borrow the name of a built-in function such as floor. Wherever you use it as a value, Varlig
reads your value:
time to work = 25 mintime to work * 10 gives 250 minpaint needed = 12.44 Lpaint needed to 1 dp gives 12.4 Lfloor = 12 m²floor * 2 gives 24 m²floor(3.7) gives 3Followed by brackets, as in floor(3.7), the function still does its usual job.
A name can even end in a number word, so a price reads the way it’s written on the menu:
table for four = £96table for four / 4 gives £24.00Names are case-sensitive: eggs and Eggs are two different values.
If you want a name available in every note, such as your hourly rate or a tax rate, add it to the global definitions in Settings rather than repeating it in each note.
Names that clash with built-in words
A name you define takes priority over anything else spelled the same way, such as a unit, a
number word or a constant. Everyday names still leave your quantities alone. A number written
straight before a plural name or a unit counts that thing, and a percentage before tip or tax
names the tip or tax, so values called hours, nights or tip don’t change these lines:
rate = £14 / hourhours = 7.5rate * 7.5 hours gives £105.00nights = 14£118/night * 14 nights gives £1,652.00tip = $12.69$84.60 + 18% tip gives $99.83Hold the pointer over the word to see which reading Varlig used. On rate * 7.5 hours it says
“Counts hours here, not the variable hours = 7.5.” Anywhere else on a line, the name is your
value, as in split £90 among people.
In a few cases your definition does change what a line means:
- A single letter always means your value, so after
m = 3,2 mis 6 rather than two metres. - A number before a singular name multiplies it, so after
width = 3,2 widthis 6. The hint suggests writing2 * widthto make that clear. - Number words and constants are replaced too: after
one = 7,one times twois 14.
Two habits avoid most surprises:
- Don’t use unit symbols on their own as names, such as
m,s,horin, or reuse constants and number words such aspi,oneori. - Prefer descriptive names like
hours worked,day rateorrun distance.
Document order
Each line sees the names defined above it. Names also carry forward into later calc blocks in the same note, so you can set up your inputs once and use them further down the page.
price = £20quantity = 3cost = price * quantity gives £60.00price = £25price * quantity gives £75.00cost gives £60.00Redefining price doesn’t change the earlier cost line: cost was worked out with the price
at that point. To change the result of an earlier calculation, edit the line where its input is
defined.
The answer above
prev is the answer on the nearest line above, and ans means the same unless you’ve named a
value ans. They suit a quick follow-up sum:
£1,250 * 12 gives £15,000.00prev / 52 gives £288.46ans + £20 gives £308.46To add up a group of lines, write sum or total on a line of its own. See
Adding up the lines above.
Scope reset
A line containing only --- starts a fresh scope. Names, custom units and your own functions
defined above it are no longer visible below it. This is useful for comparing two scenarios in
one note without their values leaking into each other.
# Option A: the flat in townrent = £1,250travel = £60rent + travel gives £1,310.00---# Option B: the flat further outrent = £980travel = £210rent + travel gives £1,190.00Below the ---, travel has to be defined again. If you forget, the line that uses it has
nothing to refer to.
Conditionals
if … then … else … picks one of two values based on a comparison. You can use it on its own or
assign the result to a name:
basket = £34.50delivery = if basket >= £40 then £0 else £3.99 gives £3.99basket + delivery gives £38.49Chain else if for more than two outcomes:
basket = £34.50if basket < £20 then £4.99 else if basket < £40 then £2.99 else £0 gives £2.99Only the chosen branch is worked out, so the other branch may contain something that would fail:
guests = 0if guests > 0 then £120 / guests else £0 gives £0.00Postfix if and unless
You can also put the condition after the value. value if condition shows the value when the
condition holds, and value unless condition shows it when the condition does not. In either
case the line shows nothing otherwise.
basket = £34.50£3.99 if basket < £40 gives £3.99£3.99 unless basket >= £40 gives £3.99Assigning inside a conditional
Both branches can assign to the same name:
basket = £34.50if basket >= £40 then shipping = £0 else shipping = £3.99 gives £3.99shipping gives £3.99With only a then branch, the assignment happens when the condition holds and the name keeps
its old value otherwise:
basket = £34.50points = 0if basket > £30 then points = 50 gives 50points gives 50Comparisons such as >= and the && and || operators are covered in
Operators, comparisons and conditionals.
Putting it together
Here is a small freelance invoice, written the way you might keep it in a note:
# March invoiceday rate = £420days worked = 6travel = £86.40 // train faresfees = day rate * days worked gives £2,520.00subtotal = fees + travel gives £2,606.40vat registered = truevat = if vat registered then subtotal * 20% else £0 gives £521.28Total due: subtotal + vat gives £3,127.68Every input has a name, so next month you only change days worked and travel, and the rest
of the invoice follows. The comments and label keep the block readable, and the conditional
means the same note works whether or not you charge VAT.