Source language: Translate to:

Add a String of Numbers and get a Total

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Add a String of Numbers and get a Total

Postby ajhunt » Mon Jan 16, 2017 9:00 am

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

Re: Add a String of Numbers and get a Total

Postby Gaev » Mon Jan 16, 2017 11:05 am

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

Re: Add a String of Numbers and get a Total

Postby ajhunt » Tue Jan 17, 2017 5:42 am

Hi Gaev, many many thanks as always - a real champion to NeoBook. Looks easy when you know how lol. Best regards.
ajhunt
 
Posts: 222
Joined: Sat Apr 23, 2005 2:33 pm
Location: United Kingdom


Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 3 guests