sriram
sriram

Reputation: 9032

How to do this in Groovy?

Lets say that there are two variable, the value of the variable is taken from the users. That is:

a=0
b=1
c=a-b

For some cases i need the variable c to be always positive, is there is any method in Groovy to do this?

Upvotes: 3

Views: 2346

Answers (1)

Gareth Davis
Gareth Davis

Reputation: 28059

Couple of options, depending on want behaviour you actually want when c is negative:

c = Math.abs(c) // -1 will become 1

c = Math.max(0,c) // -1 will become 0

// or it's an error condition
if( c < 0 ){
    tellUserToStopMessingAroundAndEnterTheCorrectValues()
    return
}

Upvotes: 5

Related Questions