Sum an array of comma-formatted numbers (e.g. 1,234)

I just spent multiple hours figuring out how to sum up an array of numbers that contain a comma as a thousand separator. And the issue was not a wrong logic but the Make UI, as I explained in a different post.

So hopefully, this guide will help other people to avoid wasting as much time as myself.

At the very end, you will also find a shared scenario I created, that should make things easier for you.

Sum an array of numbers without a comma

To better understand everything, we’ll first look at how to sum an array of numbers without a comma.

Say our input array looks like this:

[
    {
        "array": [
            {
                "value": "1234"
            },
            {
                "value": "1234"
            },
            {
                "value": "1234"
            }
        ],
        "__IMTAGGLENGTH__": 3
    }
]

To sum these values up, we first need to convert the array into a primitive array using the function {{map(X.array; “value”)}}, where X is the module number and value is the key. This results in the following output:

[
    {
        "your_variable_name": [
            "1234",
            "1234",
            "1234"
        ]
    }
]

Now we can use the sum() function as it works for primitive arrays. So both functions combined would be {{sum(map(X.array; “value”))}} and the output of this is:

[
    {
        "sum": 3702
    }
]

As you can see, everything works perfectly. However, this approach doesn’t work anymore as soon as you have number with a comma.

Sum an array of numbers with a comma

So what’s the approach for numbers with a comma? I’ll begin with the first step and always add once more function. Like this, you can easily debug your formula in case it doesn’t work.

Say our input looks like this

[
    {
        "array": [
            {
                "value": "1,234"
            },
            {
                "value": "1,234"
            },
            {
                "value": "1,234"
            }
        ],
        "__IMTAGGLENGTH__": 3
    }
]

We need to do the following:

Step 1

Convert the array to a primitive array using the map() function:

{{map(X.array; “value”)}}

Output:

[
    {
        "sum": [
            "1,234",
            "1,234",
            "1,234"
        ]
    }
]

Step 2

Join all values together to a single string using the join() function:

{{join(map(X.array; “value”); “|”)}} (note that the separator | could be replaced with any other type of separator you like)

Output:

[
    {
        "sum": "1,234|1,234|1,234"
    }
]

Step 3

Replace the comma with an empty string using the replace() function:

{{replace(join(map(X.array; “value”); “|”); “,”; “”)}}

Output:

[
    {
        "sum": "1234|1234|1234"
    }
]

Important: This step is the most critical! Why? Because of how the Make UI is made! You need to make sure that the comma , is not wrapped into quotation marks but the last argument of the function must contain quotation marks (without any content). Otherwise the function won’t work! Additionally, once you save your module, open it again and make sure that the quotation marks are still there! Make often automatically removes them when work on your formulas (see linked issue at the very top of this topic).

Step 4

Now that the comma is removed, split up the string again using the split() function:

{{split(replace(join(map(X.array; “value”); “|”); “,”; “”); “|”)}} (note, that the character for splitting the string needs to be the same character that you used for joining the string in step 2)

Output:

[
    {
        "sum": [
            "1234",
            "1234",
            "1234"
        ]
    }
]

Step 5

And finally, to sum up all the values, use the sum() function:

{{sum(split(replace(join(map(2.array; "value"); "|"); ","; ""); "|"))}}

Output:

[
    {
        "sum": 3702
    }
]

Summary

As you might have noticed: when you don’t have a comma in your array values, you can simply combine the map() function with the sum() and everything works perfectly fine. If you have commas in your values, then you additionally need to perform a join() > replace() > split() to first get rid of the commas before using the sum() function.

And once again, since this is so crucial: Make absolutely sure that you use the quotation marks correctly in the replace function, otherwise it won’t work.

Shared scenario

And to make things even easier for you, here is the link to the shared scenario