Page 1 of 1
[SOLVED] Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 5:45 am
by karmacomposer
I have four text fields for the user to enter dollar amounts - hopefully no $ (do we use the delimiter to parse out a possible "$" - must be only numbers
Here is what I wrote (I KNOW it's wrong):
Code: Select all
--Premium Subtotal Calculation
put the value of field "fldBiWeekPrem1" into varPrem1Add
put the value of field "fldBiWeekPrem2" into varPrem2Add
put the value of field "fldBiWeekPrem3" into varPrem3Add
put the value of field "fldBiWeekPrem4" into varPrem4Add
put the value of varPrem1Add+varPrem2Add+varPrem3Add+varPrem4Add into varSubtotal
set the text of field "fldPremSubTotal" to it
How do I add up fields into a subtotal field?
Mike
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 5:54 am
by ClipArtGuy
Code: Select all
repeat with x=1 to 4
add fld ("fldBiWeekPrem"&x) to tCount
put tCount into field "fldPremSubTotal"
end repeat
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 6:02 am
by karmacomposer
That threw an error in the line:
Code: Select all
add fld ("fldBiWeekPrem"&x) to tCount
The field names are the same, so fldBiWeekPrem1 and so on is correct.
Did I do something wrong?
Mike
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 7:56 am
by Klaus
Hi Mike,
That should definitively work!
What is the error exactly?
Best
Klaus
P.S.
I recommend to take a look at these stacks to get the basics of Livecode, maybe start with "Controls":
http://www.hyperactivesw.com/revscriptc ... ences.html
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 8:59 am
by Klaus
Hi Mike,
another hint which saves a lot of typing:
"the value of fld xyz" is not neccessary, only in seldom cases, just do:
Code: Select all
...
put fld "my field" into tFieldContent
...
## And
## put the value of varPrem1Add+varPrem2Add+varPrem3Add+varPrem4Add into varSubtotal
put varPrem1Add + varPrem2Add + varPrem3Add + varPrem4Add into varSubtotal
...
And SPACES do not hurt, but make the scripts more readable!
Best
Klaus
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 12:58 pm
by karmacomposer
The error was
I'll try the other way.
Mike
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 1:02 pm
by karmacomposer
The second one:
Code: Select all
put varPrem1Add + varPrem2Add + varPrem3Add + varPrem4Add into varSubtotal
set the text of field "fldPremSubTotal" to it
Gives me the error:
Code: Select all
(Operators +: error in left operand
Any other suggestions?
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 1:05 pm
by Klaus
Hm, sound like there could be non-numeric content in one of the fields.
Did you double-check their values (in the debugger)?
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 1:10 pm
by dunbarx
Clearly someone left a "$" in one of the fields.
LC cannot add '3" to "$7"
I have had this in my library since 1990:
Code: Select all
function goodNumber tText
repeat for each char theChar in tText
if theChar is in ".0123456789" then put theChar after temp
end repeat
return temp
end goodNumberr
So you would want to:
Code: Select all
put goodnumber(field "fldBiWeekPrem1") into varPrem1Add
But it would be even better to validate the input text when the user is working. Do you know how to do this?
Craig Newman
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 1:39 pm
by karmacomposer
I forgot to mention that all four fields have the following code to ensure digits only:
Code: Select all
# Allow only digits - no letters or symbols
on keyDown pKey
if pKey is not a number then
## If the parameter is not 0-9
## prevent text entry
else
## Allow text entry by passing the keyDown message
pass keyDown
end if
end keyDown
Could this change things?
Mike
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 2:01 pm
by Klaus
Hi Mike,
this is VERY strange!?
I can offer to take look at your (stripped down) stack, if you like, just send it to my email address.
And here another typing-saving advice:
Code: Select all
on keyDown pKey
if pKey is a number then
## Allow text entry by passing the keyDown message
pass keyDown
end if
end keyDown
Best
Klaus
Re: Math - ugh - How to add up fields of strings
Posted: Fri Jul 20, 2018 4:53 pm
by karmacomposer
All figured out. I found my error - and it was a biggie.
Thanks for the help guys.
Mike
Re: [SOLVED] Math - ugh - How to add up fields of strings
Posted: Sat Jul 21, 2018 10:50 pm
by dunbarx
Glad to hear you worked it out.
But just to close this properly, limiting text entry only to digits rarely works, unless only integers are desired.
What about the poor decimal point?
Craig
Re: [SOLVED] Math - ugh - How to add up fields of strings
Posted: Mon Jul 23, 2018 2:33 pm
by karmacomposer
I accounted for the decimal point as well.
Mike