Ask a Question

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
Champion Level 3

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

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


--------------------
Senior SQA Automation Engineer
gdave
Regular Contributor

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
Champion Level 3

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 Champion]
____
[Community Champions] 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 Champions]
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 Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
sonya_m
SmartBear Alumni (Retired)

Thanks everyone, great conversation!

 

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


Sonya Mihaljova
Community and Education Specialist

gdave
Regular Contributor

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

 

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

Hi @ebarbera 

 

Can this be achieved using code snippet ?

 

If yes, can you please help with possible approach ?

 

Thanks

G

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

cancel
Showing results for 
Search instead for 
Did you mean: