Page 1 of 1

Add a String of Numbers and get a Total

PostPosted: Mon Jan 16, 2017 9:00 am
by ajhunt
Hi guys, is there an easy way to add a group of 16 numbers contained in a string together and get the total value. e.g. "0123456789123456"= "66" I have looked at Ronnies rtMidString and looping and adding with math action but not getting what I want - i.e. the total of all the numbers in the string. As always any help appreciated.

Re: Add a String of Numbers and get a Total

PostPosted: Mon Jan 16, 2017 11:05 am
by Gaev
ajhunt:

Try this code ...
Code: Select all
SetVar "[theString]" "1234567890123456"
StrLen "[theString]" "[theStringLen]"

SetVar "[thisStringTotal]" "0"

Loop "1" "[theStringLen]" "[thisCharPos]"
     SubStr "[theString]" "[thisCharPos]" "1" "[thisCharValue]"
     Math "[thisCharValue]+[thisStringTotal]" "0" "[thisStringTotal]"
EndLoop


Works with any string length.

Assumes that every character in the string variable is a number ... if not, you can add a check to see if [thisCharValue] is in range 0 to 9.

Re: Add a String of Numbers and get a Total

PostPosted: Tue Jan 17, 2017 5:42 am
by ajhunt
Hi Gaev, many many thanks as always - a real champion to NeoBook. Looks easy when you know how lol. Best regards.