cancel
Showing results for 
Search instead for 
Did you mean: 

Sum of two variable values

gdave
Regular Contributor

Sum of two variable values

Hi all

 

I am trying to sum up the total of two different variables and store the value in a third variable. However, for some odd reasons, the variable (result) is reflecting the individual value of each variable and not the sum total. I am not sure what wrong have I done. Can some please advise ?

 

gdave_1-1617716403059.png

 

gdave_0-1617716261225.png

gdave_2-1617716614074.png

I would have expected a sum total of above two values to reflect in the result variable.

 

Thanks in advance.

 

11 REPLIES 11
Marsha_R
Community Hero

Re: Sum of two variable values

Your result variable is of type String, so the result you are getting is a string.  You'll need to change that to type Integer to get the sum of the original variable contents.

tphillips
Frequent Contributor

Re: Sum of two variable values

Looks like you might need to do some parsing too, to remove the commas and currency symbol


--------------------
QA Systems Architect
gdave
Regular Contributor

Re: Sum of two variable values

Hi

Thanks, it worked.

 

However I am experiencing a different issue now.

Issue 1: The property checkpoint doesn't match up but testcomplete still says that the property check point passed.

Issue 2: Is there a way I can ensure the expected value = actual value ? I've used the two variable type as integer however it seems that the sum total is rounded off automatically which has resulted in a rounded expected value. Thanks

 

gdave_0-1617788142226.png

gdave_1-1617788868754.png

 

 

AlexKaras
Community Hero

Re: Sum of two variable values

Hi,

 

You did not provide values that variables contain, but according to the checkpoint's screenshot, it is my guess that variables contain not numeric values, but some string values (currency symbol with the number).

If my guess is correct, then you should do the following:

-- Parse the value to be stored to the variable and strip everything except numeric value;

-- Set the project variable to be of double type;

-- Assign the value to the variable;

-- Repeat the above with the second value and variable;

-- Now values of project variables can be summed.

 

Note, that as you are totaling float values, the result might be not exact expected value but expected value plus/minus some delta caused by rounding. This is a usual problem for the mathematical operations over float numbers.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
sonya_m
Community Manager

Re: Sum of two variable values

Thanks everyone, great conversation!

 

Hi @gdave! Please let us know if Alex's advice helped you further. 


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
gdave
Regular Contributor

Re: Sum of two variable values

Thanks for the help.

 

Everything else worked fine. The only thing I am struggling with is parsing the value.

 

Would you be able to advise how do I go about 'Parse the value to be stored to the variable and strip everything except numeric value;'. Thanks

gdave_0-1618307132372.png

 

ebarbera
Staff

Re: Sum of two variable values

hi @gdave

 

You can trim the string using the substring or trim method of aqString.

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/substring.html

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/trim.html

 

To do this I would write a short function to store the value into a variable then trim that variable, then you can do the checkpoint based on the trimmed variable.

 

Let me know if you have any questions on how to approach this.

gdave
Regular Contributor

Re: Sum of two variable values

Hi @ebarbera 

 

Can this be achieved using code snippet ?

 

If yes, can you please help with possible approach ?

 

Thanks

G

ebarbera
Staff

Re: Sum of two variable values

hi @gdave - 

 

I think we may need a short function here based on the fact that we will have to remove the comma and the dollar symbol. 

 

What language is your project in - I would be happy to assist in writing the function. 

 

Emma

New Here?
Join us and watch the welcome video:
Top Kudoed Authors