Skip to content
MangoMap Help Centre
Esc
↑↓navigate↵open⌘Jpreview
On this page

How to use operators and functions in equations

Type operators and functions into a popup equation, such as rounding and percentages. Use the same syntax in Custom and Calculator popups.

Type an expression like round(({part} / {total}) * 100) into an equation, and each feature shows the computed result — a rounded percentage here, a different one on the next feature. The equation editor has toolbar buttons for five operators, but it’s a free-text field, so you can type any supported operator or function directly; the only rule is that the expression must work out to a single number. Equations work with numbers only: they cannot show text.

This applies to both places equations are used: the equation extension in a Custom popup, and Step 3 of a Calculator popup. The syntax is identical. For the full list of operators, functions, and formatting methods, see Calculator formula syntax.

Before you start

Enter an operator or function

For the five common operators — +, -, *, /, and parentheses — click the buttons in the modal’s Operators group, or type them. Other operators and all functions are typed directly:

  • Operators you type: modulo %, exponentiation ^, unary negative (-{field}), factorial !.
  • Functions you type: round(...), floor(...), min(...), and the rest. Names are case-sensitive — round, not Round. Put parentheses around the argument: round({population} / {area}).

Watch the Preview (with sample values) panel as you type. It calculates the expression with the values of one sample feature and shows = <number>. If it shows a message in red, check the formula before saving. See If the preview shows an error.

Basic arithmetic

The everyday operators. Parentheses force the order you want; without them, ^ runs before * / %, which run before + -.

In the tables below, the last column is the value to type in Decimal Places for that formula. It is not set by the formula. The default is 2.

Goal Formula Set Decimal Places to
Add two attributes {male} + {female} 0
Subtract one from another {total} - {male} 0
Multiply {rate} * {total} 2
Divide {total} / {households} 2
Group before dividing ({male} + {female}) / {households} 2
Negative of a value -{change} 0
Remainder after dividing {total} % {per_box} 0
Raise to a power {side} ^ 2 0

Rounding and numeric functions

Functions wrap an expression and transform its result. Rounding functions are the most common — note that Decimal Places only controls how the final number is displayed, whereas round, floor, and ceil change the value itself.

Goal Formula Set Decimal Places to
Round to the nearest whole number round({total} / {households}) 0
Always round down floor({total} / {households}) 0
Always round up ceil({total} / {households}) 0
Drop a negative sign abs({male} - {female}) 0
Smaller of two attributes min({this_year}, {last_year}) 0
Larger of two attributes max({this_year}, {last_year}) 0

Thousand separators and fixed decimals

Add a formatting method to the end of a formula to change how the result shows. .toLocaleString() adds thousand separators. .toFixed(n) shows exactly n decimals. You can use both, in either order. A formatting method replaces the Decimal Places setting.

Goal Formula Result
Whole number with thousand separators round({population} / {area}).toLocaleString() 12,508
One decimal with thousand separators roundTo({population} / {area}, 1).toLocaleString() 12,508.3
European number style round({population} / {area}).toLocaleString('de-DE') 12.508
Exactly one decimal, no separators ({part} / {total} * 100).toFixed(1) 45.7
Exactly two decimals with thousand separators ({population} / {area}).toFixed(2).toLocaleString() 12,508.28

.toLocaleString() shows up to 3 decimals, with no zeros at the end. To set the decimals, round inside the formula or add .toFixed(n).

The locale code can also show the digits in another script, such as Thai (.toLocaleString('th-TH-u-nu-thai') gives ๑๒,๕๐๘) or Arabic ('ar-EG'). For the locale codes and scripts, see Locales and number scripts. For every rule, see Format the result.

Real-world calculations

The kinds of formulas you’ll actually choose — shares, ratios, differences, and averages.

Goal Formula Set Decimal Places to
One attribute as a percentage of a total ({part} / {total}) * 100 2
Two attributes combined, as a share of a total (({a} + {b}) / {total}) * 100 2
Rounded percentage round(({part} / {total}) * 100) 0
Sum of two categories {widowed} + {divorced} 0
Difference between two categories abs({category_a} - {category_b}) 0
Difference between two measures {measure_f} - {measure_m} 2
Average of two measures ({measure_a} + {measure_b}) / 2 2

If the preview shows an error

The preview shows a message in red when the expression does not give a single number for the sample feature. Common causes:

  • Result is not a number — a comparison or logical operator on its own. {a} > {b} and {a} and {b} give true or false, not a number. To use a comparison, wrap it in a conditional: if({stock} > 0, {price}, 0) returns a number.
  • Result is not a number — text in the formula. An equation cannot show text. In if({stock} > 0, {price}, 'Sold out'), the popup shows the price when stock is above zero, and is blank for every other feature. The preview shows this message only when the sample feature takes the text branch. Both branches must be numbers. To show Sold out, use conditional content.
  • Syntax error — an unmatched parenthesis, or a function name spelled wrong (Round instead of round).
  • Field not found: {name} — the layer has no attribute with that name. Check the spelling.
  • Not a number: name — the sample feature has a text value for the attribute.
  • Division by zero — the formula divides by zero for the sample feature.

Fix the formula until the preview shows = <number>, then save.

No sample value for {name} means that the sample feature has no value for the attribute. The formula can still work for other features. Click Insert Equation, save the popup, and check the result on the map. For every message, see Preview messages.