Add numeric values together?

Hi,

In the following variable, how could I add up and get the result for the total combined sum of all the numbers here. I would like to get the total numeric value of all of these (below).


aa, 934, 1874, 85, 167, 490, 7, 31, aa, aa, 563, 163, 106, 220, 535, 13, 71, 700, aa, 83, 115, 194, 315, 654, 483, 3661, 258, 15, 82, 140, 1877, 14, 805, aa, 595, 1000, 1000, 10

Regards,
Ali

This should work, or at least give you an idea:

MACRO:   Add Numbers in a Variable [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/0/04d272ae14b8b08d4ae903fa89e21c93afd3e9ed.kmmacros">Add Numbers in a Variable [Example].kmmacros</a> (3.4 KB)

---

### ReleaseNotes

1. The numbers can contain decimal point
2. Any character other than a digit or decimal point is treated as a boundary
3. All other characters are ignored

EDIT:  2016-10-01  12:51 CT

The regex works fine, but technically you don't need the vertical bar.
YOu can replace it with this:
`[\d\.]+`

---

<img src="/uploads/default/original/2X/e/ed3c5e3feebede199e4a5ced26904ee396e0d2ae.png" width="680" height="947">
2 Likes

I really appreciate this help, fantastic.

Now with the result of 1111111.111 how could I add a ‘,’ to the thousands and remove the decimal place? so I end up with 1,111,111 ?

Thank you very much.

Insert this Action before the Display Window Action:

1 Like

Thank you very much

i can’t seem to find this action :slight_smile:

It’s the normal Set Variable to Calculation action. You just have to activate Format Result in the Gear menu:

1 Like

Hey Ali,

On my system I’d use AppleScript and the Satimage.osax.

set varStr to "aa, 934, 1874, 85, 167, 490, 7, 31, aa, aa, 563, 163, 106, 220, 535, 13, 71, 700, aa, 83, 115, 194, 315, 654, 483, 3661, 258, 15, 82, 140, 1877, 14, 805, aa, 595, 1000, 1000, 10"
set varList to find text "\\d+" in varStr with regexp, all occurrences and string result
set numSum to format ((sum of (statlist varList)) div 1) into "###,###,###,###"

-Chris

1 Like

Or, in an Execute JavaScript action:

ES6 JS (Sierra)

"aa, 934, 1874, 85, 167, 490, 7, 31, aa, aa, 563, 163, 106, 220, 535, 13, 71, 700, aa, 83, 115, 194, 315, 654, 483, 3661, 258, 15, 82, 140, 1877, 14, 805, aa, 595, 1000, 1000, 10"
.split(/[,\W]+/)
.reduce((a, x) => isNaN(x) ? a : a + parseInt(x, 10), 0)

// 17260

ES5 JS (Yosemite to Sierra)

"aa, 934, 1874, 85, 167, 490, 7, 31, aa, aa, 563, 163, 106, 220, 535, 13, 71, 700, aa, 83, 115, 194, 315, 654, 483, 3661, 258, 15, 82, 140, 1877, 14, 805, aa, 595, 1000, 1000, 10"
.split(/[,\W]+/)
.reduce(function (a, x) { return isNaN(x) ? a : a + parseInt(x, 10); }, 0)

// 17260
2 Likes