# Thread: Want to retrieve the sum by addition of values stored in 2 variables

1. ## how to get the addition to work, if you add 2+2 it writes 22 instead of 4

Hi all,

I have below code which is intended to do this

1. Check for a highest price item and retrieve the price of the item - code works and retrieves value as 104
2.Calculates the percentage of the same ( like 104 * 50/100 ) - this is working
3. Check for the second highest price item - code works and retrives value as 64 and calcualtes 40% of that

Now I want to add the values retrived in 2 variables . I have added the code but the result comes as '52 27.60' instead of '77.60' . Below is the piece of code . Will be thankful if anyone can help out

Set Desc = Description.Create()
Desc("micclass").Value = "WebElement"
Desc("class").Value = "cartRetailPrice"
set Objects=Browser("SPC").Page("SPC").WebElement("wbe lm_SC").ChildObjects(Desc)
count1 = Objects.count()
'msgbox count1
For i = 0 to count1-1
Price=Objects(i).GetROProperty("innertext")
'msgbox Price
Price1=Price1+","+Price
wait 1
Next
'msgbox Price1

'Price1 =Right (HighestPrice,1)
'Following funtion returns the higest price among all items
Function HighestValue(Price1)
Dim num, gVal,i
num = Split(Price1, ",", -1,1)
gVal = num(1)
msgbox gVal
For i = 0 to UBound(num)-1
If CInt(gVal)<CInt(num(i+1)) Then
gVal = num(i+1)
End If
Next
HighestValue = gVal
End Function

HighestPrice=HighestValue(price1)
HighestPrice =Right (HighestPrice,7)
HighestPrice=Cint(HighestPrice)
msgbox HighestPrice

'End Function
'Parameter("HighestPrice") =HighestPrice
'msgbox HighetPrice

'LTrim (HighestPrice,1)

'msgbox HighestPrice
'Calculates the Promotional Offer
'PromotionalOffer=FormatNumber(HighestPrice*50/100)
PromotionalOffer=FormatNumber(HighestPrice*50/100)
'msgbox PromotionalOffer

Set Desc = Description.Create()
Desc("micclass").Value = "WebElement"
Desc("class").Value = "Price"
set Objects=Browser("SPC").Page("SPC").WebElement("wbe lm_SC").ChildObjects(Desc)
count1 = Objects.count()
'msgbox count1
For i = 0 to count1-2
Price=Objects(i).GetROProperty("innertext")
Price2=Price
wait 1
Next
'msgbox Price2

SecondhighestPrice=mid(Price2,12,5)
SecondhighestPrice=Cint(SecondhighestPrice)
PromotionalOffer2=FormatNumber(SecondhighestPrice* 40/100)

TotalSavings=PromotionalOffer+PromotionalOffer2
-------------msgbox TotalSavings --------------------

Thanks
Bineesh Damodaran

2. Can you tell us the spot in your code that is troublesome?

3. TotalSavings= cDbl(PromotionalOffer) + cDbl(PromotionalOffer2)

PromotionalOffer value return as "54" but its datatype is string so script concatenate PromotionalOffer and PromotionalOffer2. To sum you need to change datatype as Int, dbl or some other numeric formats.
Hope this will clarify your doubt.

4. good catch Venu..

5. 1. Format Number return Expression formatted as a Number
SecondhighestPrice=10000
PromotionalOffer2=FormatNumber(SecondhighestPrice* 40/100)

msgbox TypeName(PromotionalOffer2)

2. Type Cast to other Datatype than String

SecondhighestPrice=10000
PromotionalOffer2=CInt(FormatNumber(SecondhighestP rice* 40/100))

msgbox TypeName(PromotionalOffer2)

6. try this-
TotalSavings = cDBL(PromotionalOffer) + cDbl(PromotionalOffer2)

7. run below code, it will clear your doubt--

str1="52 "
str2 = "27.60"
msgbox str1+str2 ' print 52 27.6
msgbox cDbl(str1)+cDbl(str2) ' print 79.6

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.