Forum Discussion

Wave_Crest_Hold's avatar
Wave_Crest_Hold
Contributor
15 years ago

Not able to perform Arithmatic Operation in Groovy script

Hi,

Installed 32 bit SoapUI Pro

In Groovy script:

a= props.getPropertyValue("a")
b = props.getPropertyValue("b")
c=a+b
log.info(a)
log.info(b)
log.info(c)

Result is:
Thu Feb 03 18:10:06 IST 2011:INFO:58961.0
Thu Feb 03 18:10:06 IST 2011:INFO:58951.0
Thu Feb 03 18:10:06 IST 2011:INFO:58961.058951.0

But I want "c" Result as 58961.0+58951.058951.0=117912

It is taking a and b values as strings, how can i convert these values into "Integers"?

Please help me

7 Replies

  • Jasper175's avatar
    Jasper175
    Frequent Contributor
    def a = context.expand( '${Properties#a}' )
    def b = context.expand( '${Properties#b}' )
    def c = a + b

    log.info ( a + " + " + b + " = " + c )

    That worked for me

    Rob
  • Hi Rob,

    I tried as u said

    I have 2 properties balance=58961.0 and ExpBalance=58951.0

    In Groovy:

    def a = context.expand( '${Properties#balance}' )
    def b = context.expand( '${Properties#ExpBalance}' )
    def c = a + b
    log.info ( a + " + " + b + " = " + c )

    Result coming as :

    Fri Feb 04 11:39:15 IST 2011:INFO:58961.0 + 58951.0 = 58961.058951.0

    I am not getting the total amount
  • Jasper175's avatar
    Jasper175
    Frequent Contributor
    Some QA guy I am!
    The Groovy in SoapUI seems watered down - the parameter "+" doesn't read as "plus" but rather as a string and puts them together - and it's even taking the groovy method a.plus(b) the same way.
    I got the same thing you did - I just didn't look at it... sorry about that.

    I'll get back to you if I figure it out.
  • Jasper175's avatar
    Jasper175
    Frequent Contributor
    I emailed support - I tried many java codes / groovy codes - - some how the properties file is the culprit the way it reads numerical values.
  • Hi!

    properties are stored as strings, so you'll need to convert them back to int's before adding them up, for example

    def c = Integer.parseInt( a ) + Integer.parseInt( b )

    regards!

    /Ole
    eviware.com
  • Hi Ole,

    I tried as u said

    def c = Integer.parseInt( a ) + Integer.parseInt( b ), but i am getting "java.lang.NumberFormatException:For input string:"58961.0""
  • In this particular case

    Double.parseDouble( "58961.0" )

    will do the trick (since you're dealing with floating point numbers).

    Double.parseDouble( "58961.0" )


    Hope it helps!

    /Henrik
    www.eviware.com