Add a String of Numbers and get a Total

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Locked
ajhunt
Posts: 266
Joined: Sat Apr 23, 2005 2:33 pm
Location: United Kingdom
Contact:

Add a String of Numbers and get a Total

Post 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.
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: Add a String of Numbers and get a Total

Post 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.
ajhunt
Posts: 266
Joined: Sat Apr 23, 2005 2:33 pm
Location: United Kingdom
Contact:

Re: Add a String of Numbers and get a Total

Post by ajhunt »

Hi Gaev, many many thanks as always - a real champion to NeoBook. Looks easy when you know how lol. Best regards.
Locked