Average macro

Learn how to calculate the average of a collection of numbers.

This macro calculates the average of a collection of numbers.

The average is the sum of all the numbers in the collection divided by the number of elements in the collection. For example, the average of the numbers 2, 3, 4, 7, and 9, which add up to 25, is 5.

Since averaging involves division, passing an empty collection will result in an error due to division by zero. So be sure to provide a default value in cases where the collection might be empty.

Syntax

This macro has the following syntax:

average of /*<collection>*/
Try in Playground

You can also prefix the macro with a definite article for readability:

the average of /*<collection>*/
Try in Playground

Parameters

These are the supported parameters:

collection

A non-empty collection of numbers you want to average. Elements are automatically converted to numbers if necessary.

Examples

Here is an example of how to calculate the average of a collection of numbers:

average of [1.5, "2.5", 3.5] // 2.5
Try in Playground

In cases where the collection might be empty, you can provide a default value:

average of /*<collection>*/ or else 0
Try in Playground