How to do addition of two variables contains value like 17.72 and 26.5


#1

Hi,

scenario is including following steps:

  1. Take the cost value in one variable and do same for all cost present on the page
  2. Add all the variables.
  3. Compare with total cost displayed on page.

I am not able to add the variables.

Please help me.

Thanks,
Sristi Sharma.


#2

You probably use WebUI.getText() to get numbers, so you must convert String variable to float/double.

String var1 = "17.5"
String var2 = "5.555"

Double number1 = Double.parseDouble(var1)
Double number2 = Double.parseDouble(var2)

println number1 + number2

#3

Hi @Marek_Melocik,

Can I store the result of number1 + number2 in one variable?

Thank u for your quick reply.


#4

Of course, but the variable must be Double type (and it could be casted afterwards if you want)

Double result = number1 + number2

// if you want to have String result
String resultAsString = result.toString()

#5

Hi @Marek_Melocik,

I have tried it and it works,

Thank You.:slight_smile: